【问题标题】:Stream File to Chome Broken, Firefox Working流文件到 Chome 损坏,Firefox 工作
【发布时间】:2011-01-22 01:31:06
【问题描述】:

我编写了一个 ASP.Net 网页,它将采用 QueryString 并将文件流式传输到客户端。该文件存储在 SQL Server 数据库中。当我在开发过程中在本地运行网站时,一切都很好。当我从服务器在生产中运行它时,我可以通过 Firefox 获取文件,但不能通过 Chrome 获取。在 Chrome 中我得到Error 100 (net::ERR_CONNECTION_CLOSED): Unknown error.

查看其他一些提到这可能与Content-Length 相关的帖子,但是,我不明白为什么这会在开发而不是生产中起作用。出于这个原因,我认为这里一定有其他事情发生。

感谢您的任何建议/提示。

这是我的代码:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim Data_ID As String = Request.QueryString("Data_ID")

    Using dt As New Enterprise_Error_Log.Field_FileDataTable
        Using ta As New Field_FileTableAdapter
            ta.Fill(dt, Data_ID)

            If dt.Rows.Count > 0 Then
                Dim myRow As Enterprise_Error_Log.Field_FileRow = dt.Rows(0)
                Dim myFileName As String = myRow("Field_File_Name")
                'Dim myFileData() As Byte = myRow("Field_File")
                Dim myFileLength As String = myRow("Field_File_Length")

                Response.BufferOutput = False
                Response.AddHeader("Content-Disposition", "inline;filename=""" & myFileName & """")
                Response.AddHeader("Content-Length", myFileLength)

                StreamFile(Data_ID)

                Response.Close()

            End If

        End Using
    End Using

End Sub

Private Sub StreamFile(ByVal Data_ID As String)
    Dim bolGotContentType As Boolean = False

    Using conn = New SqlConnection(Enterprise_Error_LogConnectionString.ConnectionString)
        Using cmd = conn.CreateCommand()
            conn.Open()
            cmd.CommandText = "SELECT Field_File FROM Ext_Error_Log WHERE (Data_ID = @Data_ID)"
            cmd.Parameters.AddWithValue("@Data_ID", Data_ID)

            Using reader = cmd.ExecuteReader(Data.CommandBehavior.SequentialAccess)
                While reader.Read()
                    Dim buffer As Byte() = New Byte(8040) {}
                    ' Read chunks of 1KB
                    Dim bytesRead As Long = 0
                    Dim dataIndex As Long = 0
                    Do
                        'read next chunk
                        bytesRead = reader.GetBytes(0, dataIndex, buffer, 0, buffer.Length)
                        If bytesRead > 0 Then
                            'advance index
                            dataIndex += bytesRead

                            'if this is the first chunk, get the mime type from it.
                            If Not bolGotContentType Then
                                Response.ContentType = "application/octet-stream" 'getMimeFromFile(buffer)
                                bolGotContentType = True
                            End If

                            Response.BinaryWrite(buffer)
                            Response.Flush()
                        End If
                    Loop Until bytesRead = 0

                End While

            End Using
        End Using
    End Using


End Sub

我的标题如下:

Cache-Control: private
Date: Mon, 24 Jan 2011 20:37:33 GMT

Content-Length: 3153269
Content-Type: application/octet-stream

Content-Disposition: attachment;filename="C:\Users\CBARTH\AppData\Local\Temp\tmp4BC8.mdmp.gz";size=3153169
Server: Microsoft-IIS/6.0
X-AspNet-Version: 2.0.50727
X-Powered-By: ASP.NET

Content-Encoding: gzip

【问题讨论】:

  • 您用于填充 Content-Disposition 的代码已损坏;您可能需要引用该值(例如,当文件名包含空格时)甚至转义(对于非 ISO-8859-1)
  • 该值被引用,即使标题检查显示它被引用。也许我错过了您所指的内容。

标签: asp.net http http-streaming http-status-code-100


【解决方案1】:

我建议检查 HTTP 跟踪工具中的实际响应(想到 Firefox LiveHTTP 标头),并检查 Content-Length 是否发生了奇怪的事情(设置多次?)

【讨论】:

  • 我用了Fiddler2,看了看,没有发现headers有什么问题。但是我注意到 IIS 使用 gzip 进行响应,并且我的内容没有经过 gzip 压缩。我修改了我的代码以压缩内容,现在 Firefox 和 Chrome 都在下载文件。但是,它们都不是以百分比报告进度,它们只是报告下载的字节数。此外,Fiddler2 报告了 Content-Length 不匹配,并且声明的 Content-Length 小于实际的 Content-Length。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多