asp.net – 来自asp app的流媒体mime类型’application / pdf’在Google Chrome中失败

前端之家收集整理的这篇文章主要介绍了asp.net – 来自asp app的流媒体mime类型’application / pdf’在Google Chrome中失败前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个Web应用程序,可以在点击事件中流式传输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,Size);
        while (numBytes > 0)
        {
            output.Write(bytes,numBytes);
            numBytes = input.Read(bytes,Size);
        }

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

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

解决方法

最新的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,Size);
totalBytes += numBytes;
while (numBytes > 0)
{
    output.Write(bytes,numBytes);
    numBytes = input.Read(bytes,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();

猜你在找的asp.Net相关文章