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

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....