微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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();

原文地址:https://www.jb51.cc/aspnet/249174.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐