从ASP应用程序流的MIME类型“应用程序/ PDF”谷歌浏览器失败 [英] Streaming mime type 'application/pdf' from asp app fails in Google Chrome

查看:105
本文介绍了从ASP应用程序流的MIME类型“应用程序/ PDF”谷歌浏览器失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个click事件流的PDF文件的Web应用程序,它工作正常在IE,Firefox和Safari,但在Chrome它从来没有下载。下载只是写着追访。 Chrome是否处理流不同?我的code如下:

I have a web application that streams a PDF file on a click event, it works fine in IE, Firefox, and Safari but in Chrome it never download. The download just reads "Interrupted". Does Chrome handle streaming differently? My code looks like:

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

任何建议,我可能会错过了什么?

Any suggestions as to what I might be missing?

推荐答案

最近的谷歌浏览器的V12版本的出台触发您所描述问题的错误

A recent Google Chrome v12 release introduced a bug that triggers the problem you describe.

您可以通过发送内容长度头,在你的code以下修改后的版本修复它

You can fix it by sending the Content-Length header, as in the following modified version of your code:

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

这篇关于从ASP应用程序流的MIME类型“应用程序/ PDF”谷歌浏览器失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆