【问题标题】:Streaming mime type 'application/pdf' from asp app fails in Google Chrome来自 asp 应用程序的流式 mime 类型“应用程序/pdf”在 Google Chrome 中失败
【发布时间】:2011-09-13 05:54:52
【问题描述】:

我有一个网络应用程序,可以在点击事件上流式传输 PDF 文件,它在 IE、Firefox 和 Safari 中运行良好,但在 Chrome 中它从不下载。下载只是显示“中断”。 Chrome 处理流媒体的方式不同吗?我的代码如下:

        this.Page.Response.Buffer = true;
        this.Page.Response.ClearHeaders();
        this.Page.Response.ClearContent();
        this.Page.Response.ContentType = "application/pdf";
        this.Page.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
        Stream input = reportStream;
        Stream output = this.Page.Response.OutputStream;
        const int Size = 4096;
        byte[] bytes = new byte[4096];
        int numBytes = input.Read(bytes, 0, Size);
        while (numBytes > 0)
        {
            output.Write(bytes, 0, numBytes);
            numBytes = input.Read(bytes, 0, Size);
        }

        reportStream.Close();
        reportStream.Dispose();
        this.Page.Response.Flush();
        this.Page.Response.Close();

关于我可能缺少什么的任何建议?

【问题讨论】:

    标签: asp.net google-chrome mime-types


    【解决方案1】:

    最近的 Google Chrome v12 版本 introduced a bug 触发了您描述的问题。

    您可以通过发送 Content-Length 标头来修复它,如以下修改后的代码版本:

    this.Page.Response.Buffer = true;
    this.Page.Response.ClearHeaders();
    this.Page.Response.ClearContent();
    this.Page.Response.ContentType = "application/pdf";
    this.Page.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
    Stream input = reportStream;
    Stream output = this.Page.Response.OutputStream;
    const int Size = 4096;
    byte[] bytes = new byte[4096];
    int totalBytes = 0;
    int numBytes = input.Read(bytes, 0, Size);
    totalBytes += numBytes;
    while (numBytes > 0)
    {
        output.Write(bytes, 0, numBytes);
        numBytes = input.Read(bytes, 0, Size);
        totalBytes += numBytes;
    }
    
    // You can set this header here thanks to the Response.Buffer = true above
    // This header fixes the Google Chrome bug
    this.Page.Response.AddHeader("Content-Length", totalBytes.ToString());
    
    reportStream.Close();
    reportStream.Dispose();
    this.Page.Response.Flush();
    this.Page.Response.Close();
    

    【讨论】:

    【解决方案2】:

    这只是一个猜测。在 chrome 中,当您在 HTTP 标头中的 Accept 或 Content-Type 中指定了多种格式时,它会使用逗号而不是分号来分隔它们(分号是标准)。当出现逗号时,某些框架实际上几乎每个框架都无法解析并抛出堆栈跟踪。您可以通过在 chrome 中使用 firebug 来验证情况并非如此。

    【讨论】:

      【解决方案3】:

      看起来 Chrome 倾向于拆分请求并分段请求文件。这可能是你的问题的症结所在,它与我有关。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-17
        • 2015-08-11
        • 2015-01-06
        • 2023-03-23
        • 2012-02-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多