Chrome 将流式文件保存为 .gz
我一直在用 Chrome 上的这种奇怪行为把头撞在墙上,
我的文件以未命名、无序的状态加密存储在磁盘上。当用户下载文件时,客户端将被重定向到下载处理程序 (.ashx),数据将加载到流中、解密并发送到客户端。现在,直到最近,它都工作得很好,
它在所有浏览器上都工作得很好,除了 chrome,其中文件作为 .gz 文件下载并且不可读?
为了简洁起见,我已经去掉了流获取和加密部分(请记住,这在 IE、Firefox 等上完美运行)
Private Function StreamFile(ByVal context As System.Web.HttpContext) As Boolean
context.Response.Clear()
context.Response.ClearHeaders()
context.Response.BufferOutput = False
context.Response.ContentType = "application/octet-stream"
context.Response.AddHeader("content-disposition", "attachment;filename=""" & _sFileName & """")
context.Response.AddHeader("content-length", _iSize)
context.Response.Cache.SetNoServerCaching()
context.Response.Cache.SetMaxAge(System.TimeSpan.Zero)
Dim oInputStream As System.IO.Stream = GetStream()
Dim oBufferedStream As New BufferedStream(oFileStream, context.Response.OutputStream)
oBufferedStream.ProcessStreams(True)
context.ApplicationInstance.CompleteRequest()
Return True
End Function
Public Class BufferedStream
Private _oInputStream As Stream
Private _oOutputStream As Stream
Private _bBuffer() As Byte
Public Const DEFAULT_BUFFER_SIZE As Integer = 8192
Public Sub New(ByRef oInputStream As Stream, ByRef oOutputStream As Stream)
_oInputStream = oInputStream
_oOutputStream = oOutputStream
_bBuffer = GetStreamBuffer(ToInteger(_oInputStream.Length))
End Sub
Public Function ProcessStreams(ByVal bCloseStreams As Boolean) As Boolean
Dim iRead As Integer = 0
Dim iStreamLength As Integer = ToInteger(_oInputStream.Length)
Try
iRead = _oInputStream.Read(_bBuffer, 0, _bBuffer.Length)
While iRead > 0
_oOutputStream.Write(_bBuffer, 0, _bBuffer.Length)
iRead = _oInputStream.Read(_bBuffer, 0, _bBuffer.Length)
If iRead < _bBuffer.Length Then System.Array.Resize(_bBuffer, iRead)
End While
If bCloseStreams Then
Close()
End If
Return True
Catch
Return False
End Try
End Function
Public Shared Function GetStreamBuffer(ByVal iStreamSize As Integer) As Byte()
Dim iBufferSize As Integer = iStreamSize - 1
If iBufferSize > DEFAULT_BUFFER_SIZE Then iBufferSize = DEFAULT_BUFFER_SIZE
Dim bBuffer As Byte() = New Byte(iBufferSize) {}
Return bBuffer
End Function
End Class
任何帮助将不胜感激。
这是在 chrome 中下载文件时来自 fiddler 的 HTTP 标头,
HTTP/1.1 200 OK
Date: Fri, 30 Oct 2009 00:01:45 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
content-disposition: attachment;filename="codeKiwi.txt"
Content-Length: 349
Cache-Control: private, max-age=0
Content-Type: application/octet-stream
请注意,文件名是 codekiwi.txt,内容是一些简单的纯文本,文件保存为 codekiwi.txt.gz 并且包含乱码。
I have been banging my head against the wall with this odd behaviour on Chrome
I have files stored encrypted on disk in an unnamed, unordered state. when the files are downloaded by the user, the client is redirected to a download handlers (.ashx) the data is loaded into a stream, decrypted and sent to the client. Now this has worked fine until recently,
It works fine on all browsers except chrome where the file is downloaded as a .gz file and is unreadable?
I have stripped out the stream fetching and cryptography parts for brevity (keep in mind this works perfectly on IE, Firefox etc)
Private Function StreamFile(ByVal context As System.Web.HttpContext) As Boolean
context.Response.Clear()
context.Response.ClearHeaders()
context.Response.BufferOutput = False
context.Response.ContentType = "application/octet-stream"
context.Response.AddHeader("content-disposition", "attachment;filename=""" & _sFileName & """")
context.Response.AddHeader("content-length", _iSize)
context.Response.Cache.SetNoServerCaching()
context.Response.Cache.SetMaxAge(System.TimeSpan.Zero)
Dim oInputStream As System.IO.Stream = GetStream()
Dim oBufferedStream As New BufferedStream(oFileStream, context.Response.OutputStream)
oBufferedStream.ProcessStreams(True)
context.ApplicationInstance.CompleteRequest()
Return True
End Function
Public Class BufferedStream
Private _oInputStream As Stream
Private _oOutputStream As Stream
Private _bBuffer() As Byte
Public Const DEFAULT_BUFFER_SIZE As Integer = 8192
Public Sub New(ByRef oInputStream As Stream, ByRef oOutputStream As Stream)
_oInputStream = oInputStream
_oOutputStream = oOutputStream
_bBuffer = GetStreamBuffer(ToInteger(_oInputStream.Length))
End Sub
Public Function ProcessStreams(ByVal bCloseStreams As Boolean) As Boolean
Dim iRead As Integer = 0
Dim iStreamLength As Integer = ToInteger(_oInputStream.Length)
Try
iRead = _oInputStream.Read(_bBuffer, 0, _bBuffer.Length)
While iRead > 0
_oOutputStream.Write(_bBuffer, 0, _bBuffer.Length)
iRead = _oInputStream.Read(_bBuffer, 0, _bBuffer.Length)
If iRead < _bBuffer.Length Then System.Array.Resize(_bBuffer, iRead)
End While
If bCloseStreams Then
Close()
End If
Return True
Catch
Return False
End Try
End Function
Public Shared Function GetStreamBuffer(ByVal iStreamSize As Integer) As Byte()
Dim iBufferSize As Integer = iStreamSize - 1
If iBufferSize > DEFAULT_BUFFER_SIZE Then iBufferSize = DEFAULT_BUFFER_SIZE
Dim bBuffer As Byte() = New Byte(iBufferSize) {}
Return bBuffer
End Function
End Class
Any help would be greatly appreciated.
Here is the HTTP header from fiddler when the file is downloaded in chrome
HTTP/1.1 200 OK
Date: Fri, 30 Oct 2009 00:01:45 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
content-disposition: attachment;filename="codeKiwi.txt"
Content-Length: 349
Cache-Control: private, max-age=0
Content-Type: application/octet-stream
Note that the filename is codekiwi.txt and the contents are some simple plain text, the file gets saved as codekiwi.txt.gz and contains gibberish.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我自己解决了。我发现Chrome(和Firefox)真的不喜欢response.ClearHeaders调用,出于某种原因IE对此没问题,一旦我删除它,下载就会再次按预期运行......
Ok, solved it myself. I turns out that Chrome (and Firefox) REALLY don't like the response.ClearHeaders call, for some reason IE was ok with this, once I removed it the downloads functioned as expected again...