简体   繁体   English

遇到使用asp.net中的流将文件下载到客户端的问题

[英]Facing an issue downloading a file to client using stream in asp.net

I was trying to create (stream) and download a html file on the client in asp.net using Http Response. 我试图使用Http Response在asp.net的客户端上创建(流)并下载html文件。

Went through many related posts here but nothing really helped. 在这里浏览了许多相关文章,但没有任何帮助。 After spending almost over two days, I stumbled upon a solution. 花了将近两天的时间后,我偶然发现了一个解决方案。

However, would now want to understand the reason, why it failed earlier. 但是,现在想了解原因,为什么它较早失败。

Here is the story: 这是故事:

Please consider the following ASP.net codebehind handler.. 请考虑以下ASP.net代码隐藏处理程序。

protected void UploadButton_Click(object sender, EventArgs e)
{
    if (FileUploadControl.HasFile)
    {
        try
        {
            if (FileUploadControl.PostedFile.ContentType == MIMEAssistant.GetMIMEType(".xls"))
            {
                string filename = "log.htm";
                string logContent = "This is a test log..";

                // Some upload processing..

                // Send log file for download
                Response.Clear();
                Response.ContentType = MIMEAssistant.GetMIMEType(filename);
                Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename);

                using (StreamWriter writer = new StreamWriter(Response.OutputStream, Encoding.ASCII))
                {
                    writer.Write(logContent);
                }

                //Response.OutputStream.Write(buf1, 0, buf1.Length);
                HttpContext.Current.ApplicationInstance.CompleteRequest();
                Response.End();
            }
            else
                StatusLabel.Text = "Upload status: Only .xls files are accepted!";
        }
        catch (Exception ex)
        {
            StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
        }
    }
}

Nothing really helped to make IE download the file and save it from getting stuck. IE并没有真正帮助使IE下载文件并避免卡死。 In the end, extracting the code under "// Send log file for download" from innermost to outermost in the function resolved the issue. 最后,从函数的最内层到最外层提取“ //发送要下载的日志文件”下的代码即可解决该问题。

EDIT: Facing the issue again even after removing the culprits explained below.. 编辑:即使删除了下面解释的罪魁祸首,仍然再次面临问题。

Please help me understand why? 请帮我理解为什么?

Note: In the code above, the statement Response.End() threw an exception that read : 注意:在上面的代码中,语句Response.End()引发了一个异常,内容为:

[System.Threading.ThreadAbortException] = {Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.} [System.Threading.ThreadAbortException] = {由于代码已优化或本机框架位于调用堆栈的顶部,因此无法评估表达式。}

More info: After further investigation and creating a sample application, I found that putting the download code inside the following code causes the issue: 更多信息:在进一步研究并创建了示例应用程序之后,我发现将下载代码放入以下代码会导致此问题:

if (FileUploadControl.HasFile)
 {
     try
     {
         this.HtmlLogDownload();
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }

It works fine if I remove: 如果删除,效果很好:

if (FileUploadControl.HasFile) 如果(FileUploadControl.HasFile)

Here is the webform code too: 这也是网络表单代码:

<form id="form1" runat="server">
    <asp:FileUpload ID="FileUploadControl" runat="server" />
    <asp:Button runat="server" ID="UploadButton" Text="Upload" OnClick="UploadButton_Click" />
    <div>

    </div>
    </form>

Edit: The above works in Chrome and Mozilla. 编辑:以上工作在Chrome和Mozilla中。 Fails in IE 11 IE 11中失败

I noticed IE11 returns an uploaded .png image as: "application/png" when checking it like this: LabelStatus.Text = "Ongeldig afbeeldingstype: " & FileUploadKleinLogo.PostedFile.ContentType 我注意到IE11像这样检查时,返回的上传的.png图像为:“ application / png”:LabelStatus.Text =“ Ongeldig afbeeldingstype:”&FileUploadKleinLogo.PostedFile.ContentType

It used to be "image/png" 以前是“ image / png”

My guess is that that's whats causing the issue in IE11 and not FF or Chrome 我的猜测是那是导致IE11而不是FF或Chrome出现问题的原因

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM