开发者

Stream File to Chome Broken, Firefox Working

开发者 https://www.devze.com 2023-02-06 06:42 出处:网络
I wrote an ASP.Net web page that will take a QueryString and stream a file to the client. The file is stored in a SQL Server database. Everything works great when I\'m running the web site locally dur

I wrote an ASP.Net web page that will take a QueryString and stream a file to the client. The file is stored in a SQL Server database. Everything works great when I'm running the web site locally during development. When I run it in production from the server I can get a file through Firefox, but not Chrome. In Chrome I get Error 100 (net::ERR_CONNECTION_CLOSED): Unknown error.

See some other posts that mention this could be related to Content-Length, however, I can't understand why this would work in development and not production. For that reason I think there must be something else going on here.

Thanks for any suggestions/hints.

Here is my code:

    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

My headers are as follows:

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


I recommend inspecting the actual response in an HTTP trace tool (Firefox LiveHTTP headers comes to mind), and to check whether something weird with Content-Length is going on (set multiple times?)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号