asp.net大文件下载内存不足异常

时间:2017-04-11 18:04:05

标签: asp.net vb.net iis-7

我试图在asp.net页面中下载大小超过50mb的文件。但它在我们的生产服务器上失败了。它适用于开发和QA服务器。我使用以下代码。

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'的类型异常被扔了。 在System.IO.BinaryReader.ReadBytes(Int32计数) at ExportDoc.Page_Load(Object sender,EventArgs e)位于c:\ sitename \ ExportDoc.aspx.vb:第87行服务器名称

代码有什么问题?

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分: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