2011-06-12 25 views
2

我有一个Web应用程序,在单击事件上流式传输PDF文件,它在IE,Firefox和Safari中正常工作,但在Chrome中它永远不会下载。下载只是“中断”。 Chrome是否处理流式传输?我的代码看起来像:从Google应用程序流式MIME类型'应用程序/ pdf'在谷歌浏览器中失败

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

有关我可能会丢失什么的任何建议?

回答

0

这只是一个猜测。在chrome中,当您在HTTP头中的Accept或Content-Type中指定了多种格式时,它使用逗号代替分号(分号是标准)来分隔它们。当用逗号表示时,一些框架,实际上几乎每个框架都无法解析并抛出堆栈跟踪。您可以通过在chrome中使用萤火虫来验证情况并非如此。

5

最近的Google Chrome v12版本introduced a bug触发了您描述的问题。

你可以在你的代码的以下修改版本发送内容长度头,为解决这个问题:

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

有用的*** Android ***和_Chrome_:'Android和HTTP下载文件头' http://www.digiblog.de/2011/04/android-and-the-download-file-headers/ http://stackoverflow.com/questions/4674737/avoiding-content-type-issues-when-downloading-a-file-via-browser-on-android/5728859#comment46671021_5728859 – Kiquenet 2017-01-25 15:39:56

0

它看起来像铬趋于分裂的请求,并要求在文件件。这可能是你的问题的关键,它与我同在。

相关问题