C# 从asp文件重定向到aspx文件后无法下载文件

C# 从asp文件重定向到aspx文件后无法下载文件,c#,asp.net,asp-classic,C#,Asp.net,Asp Classic,我有asp经典文件,它重定向到下载excel文件的aspx文件 当asp重定向到aspx文件时,不会发生任何事情 但是,如果我获得指向aspx的链接并在“新建”选项卡中打开它,则一切正常: ASP代码: Dim NodeIDsCSV : NodeIDsCSV = "" Dim NodeIDsCSV = Init_PrepareParams() Dim sharedSecret : sharedSecret = "" Dim identityAlias

我有asp经典文件,它重定向到下载excel文件的aspx文件

当asp重定向到aspx文件时,不会发生任何事情

但是,如果我获得指向aspx的链接并在“新建”选项卡中打开它,则一切正常:

ASP代码:

Dim NodeIDsCSV : NodeIDsCSV = ""
Dim NodeIDsCSV = Init_PrepareParams()


Dim sharedSecret : sharedSecret = ""
Dim identityAlias : identityAlias = ""

sharedSecret = GetSharedSecret()

identityAlias = GetIdentityAlias()


Response.Redirect "/testCMS/testCMSv2.aspx?NodeIDsCSV=" & NodeIDsCSV & "&SS=" & sharedSecret & "&IdentityAlias=" & identityAlias 
ASPX代码:

string[] nodes = NodeIDsCSV.Split(',');
   

    
    //Create a stream for the file
    Stream stream = null;

    //This controls how many bytes to read at a time and send to the client
    int bytesToRead = 100000;

    // Buffer to read bytes in chunk size specified above
    byte[] buffer = new Byte[bytesToRead];
    var fileName = "test.xlsx";
    // The number of bytes read
    try
    {
        //Create a WebRequest to get the file
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            
        HttpWebRequest fileReq = (HttpWebRequest) HttpWebRequest.Create("--link--to--service--which-returns-the-file");
        fileReq.ProtocolVersion = HttpVersion.Version10;
        //Create a response for this request
        HttpWebResponse fileResp = (HttpWebResponse) fileReq.GetResponse();

        if (fileReq.ContentLength > 0)
            fileResp.ContentLength = fileReq.ContentLength;

        //Get the Stream returned from the response
        stream = fileResp.GetResponseStream();

        // prepare the response to the client. resp is the client Response
        var resp = HttpContext.Current.Response;
        //resp.ClearHeaders();
        //Indicate the type of data being sent
        resp.ContentType = "application/octet-stream";

        //Name the file 
        resp.Clear();
        //resp.Buffer = false;
        //resp.BufferOutput = false;
        
        //Apparently this line helps with old version of IE that like to cache stuff no matter how much you tell them!
        resp.AddHeader("Pragma", "public");
        //HttpContext.Current.Response.Write(fileName + "----fileName----");
        resp.AddHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
        resp.AddHeader("Content-Length", fileResp.ContentLength.ToString());
        resp.AddHeader("Content-Transfer-Encoding", "binary");
        
        int length;


        do
        {
            // Verify that the client is connected.
            
            if (resp.IsClientConnected)
            {
            
                // Read data into the buffer.
                length = stream.Read(buffer, 0, bytesToRead);

                // and write it out to the response's output stream
                resp.OutputStream.Write(buffer, 0, length);
    
                // Flush the data
                //resp.Flush();
                
                //Clear the buffer
                buffer = new Byte[bytesToRead];
                
            }
            else
            {
                // cancel the download if client has disconnected
                length = -1;
            }
        } while (length > 0); //Repeat until no data is read
        
    }
我有什么问题?你能给我一些不同的解决方案吗


谢谢

您是否尝试过使用浏览器开发工具来确保经典ASP页面按预期重定向,以及ASPX页面如何响应?是@JackA。我试过了,aspx返回文件的xml,但是在重定向之后,文件没有被下载。但是,当我直接获得指向浏览器的链接并跳过重定向时,文件已成功下载。如果知道正在下载Excel文件,为什么要将响应设置为
application/octet stream
?很难相信您没有收到任何错误,并且无法从“开发工具”的“网络”选项卡中确定任何内容,您是否有“保留日志”勾选,以便在“网络”选项卡中持久化请求?