Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net Chrome将流文件保存为.gz_Asp.net_Vb.net_Google Chrome_Gzip_Stream - Fatal编程技术网

Asp.net Chrome将流文件保存为.gz

Asp.net Chrome将流文件保存为.gz,asp.net,vb.net,google-chrome,gzip,stream,Asp.net,Vb.net,Google Chrome,Gzip,Stream,我在铬合金上的这种奇怪行为一直让我的头撞在墙上 我将文件以未命名、无序的状态加密存储在磁盘上。当用户下载文件时,客户端被重定向到下载处理程序(.ashx)。数据被加载到流中,解密并发送到客户端。直到最近,这一切都很好 除了chrome之外,它在所有浏览器上都可以正常工作,chrome的文件是以.gz文件下载的,无法读取 为了简洁起见,我去掉了流获取和加密部分(请记住,这在IE、Firefox等上非常有效) 请注意,文件名是codekiwi.txt,内容是一些简单的纯文本,该文件保存为codeki

我在铬合金上的这种奇怪行为一直让我的头撞在墙上

我将文件以未命名、无序的状态加密存储在磁盘上。当用户下载文件时,客户端被重定向到下载处理程序(.ashx)。数据被加载到流中,解密并发送到客户端。直到最近,这一切都很好

除了chrome之外,它在所有浏览器上都可以正常工作,chrome的文件是以.gz文件下载的,无法读取

为了简洁起见,我去掉了流获取和加密部分(请记住,这在IE、Firefox等上非常有效)


请注意,文件名是codekiwi.txt,内容是一些简单的纯文本,该文件保存为codekiwi.txt.gz并包含乱码。

好的,我自己解决了。我发现Chrome(和Firefox)真的不喜欢这个响应。ClearHeaders调用,出于某种原因IE对此没有意见,一旦我删除了它,下载功能又像预期的那样运行了

    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
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