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