2017-04-11 35 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