2017-04-11 65 views
1

我試圖在asp.net頁面下載大於50mb大小的文件。但它在我們的生產服務器上失敗。它適用於開發和QA服務器。我使用下面的代碼。asp.net大文件下載內存不足異常

Response.Clear() 
oBinaryReader = New System.IO.BinaryReader(System.IO.File.OpenRead(sDocPath)) 
lFileSize = Microsoft.VisualBasic.FileLen(sDocPath) 
Response.AddHeader("Content-Disposition", "attachment;filename=" & sDownloadFileName) 
Response.ContentType = "application/unknown" 
Response.BinaryWrite(oBinaryReader.ReadBytes(lFileSize)) 
Response.Flush() 
HttpContext.Current.ApplicationInstance.CompleteRequest() 
Response.End() 

我從服務器收到的錯誤如下。

Page_Load System.OutOfMemoryException:拋出類型'System.OutOfMemoryException'的例外。 在在C System.IO.BinaryReader.ReadBytes(的Int32計數) 在ExportDoc.Page_Load(對象發件人,EventArgs的):\網站名稱\ ExportDoc.aspx.vb:行87服務器名稱

什麼是錯的碼?

+0

您沒有正確處理您的[IDiposable](https://msdn.microsoft.com/en-us/library/system.idisposable(v = vs.110).aspx)對象。 – mason

回答

0

我試過下面的代碼,它解決了我的問題,從MSDN網站上找到代碼想法。

    Using iStream As System.IO.Stream = New System.IO.FileStream(sDocPath, System.IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read) 
         dataToRead = iStream.Length 
         Response.ContentType = "application/octet-stream" 
         Response.AddHeader("Content-Disposition", "attachment; filename=" & filename) 
         While dataToRead > 0 
          If Response.IsClientConnected Then 
           length = iStream.Read(buffer, 0, bufferSize) 
           Response.OutputStream.Write(buffer, 0, length) 
           Response.Flush() 
           ReDim buffer(bufferSize) 
           dataToRead = dataToRead - length 
          Else 
           dataToRead = -1 
          End If 
         End While 
         HttpContext.Current.ApplicationInstance.CompleteRequest() 
        End Using 
1

OutOfMemoryException當處理託管/非託管資源時沒有可用內存來執行此操作時,通常會引發此錯誤。因此,你需要使用Using...End Using塊周圍包裹BinaryReader,以保證使用後立即處置非託管資源與IDisposable接口:

Response.Clear() 

Using oBinaryReader As BinaryReader = New BinaryReader(File.OpenRead(sDocPath)) 
    lFileSize = FileLen(sDocPath) 

    Response.AddHeader("Content-Disposition", "attachment;filename=" & sDownloadFileName) 
    Response.ContentType = "application/unknown" 
    Response.BinaryWrite(oBinaryReader.ReadBytes(lFileSize)) 
    Response.Flush() 
    HttpContext.Current.ApplicationInstance.CompleteRequest() 
    Response.End() 
End Using 

BinaryReader另一種常見的用法是使用FileStream和字節的緩衝區來控制文件讀取機制:

Using FStream As FileStream = New FileStream(File.OpenRead(sDocPath)) 
    lFileSize = CType(FStream.Length, Integer) 
    Dim Buffer() As Byte 

    Using oBinaryReader As BinaryReader = New BinaryReader(FStream) 
     Buffer = oBinaryReader.ReadBytes(lFileSize) 
    End Using 

    Response.Clear() 
    Response.AddHeader("Content-Disposition", "attachment;filename=" & sDownloadFileName) 
    Response.ContentType = "application/unknown" 
    Response.BinaryWrite(Buffer) 
    Response.Flush() 
    HttpContext.Current.ApplicationInstance.CompleteRequest() 
    Response.End() 
End Using 

參考文獻:

VB.NET Using Statement (MSDN)

BinaryReader Class (MSDN)

+0

這是一個很好的建議,但它不能解決我的問題。它適用於另外兩臺服務器。這臺服務器有問題。我已驗證所有配置設置,這些設置在服務器之間是相同的。 – blue