下载大文件时摆脱记忆例外
我开发了ASP.NET Web应用程序。我正在尝试从文件服务器下载文档。文件大小可能超过1GB。
这里是,第一次下载文件。当我单击相同的链接时,我会得到“不记忆异常”会引发错误。
如果是小型文件,我没有任何问题可以下载填充物...
如果我下载文档超过1GB,我们该如何处理此错误...?
以下是在ASP.NET中下载文件的代码(文件背后的代码)
Dim binFile As Byte() = File.ReadAllBytes(filename)
Response.ContentType = "application/octet-stream"
Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename)
Response.BinaryWrite(binFile)
Response.End()
I developed ASP.NET web application. I am trying to download the document from file server. FileSize maybe more than 1GB.
Here the thing is, very first time file got downloaded. when i click the same link, i am getting "Out of memory Exception" throws error.
If it is small size file, i don't get any issues to download the filles...
How do we handle this error if i download the document more than 1GB...?
below is the code to download the file in ASP.NET (Code behind file)
Dim binFile As Byte() = File.ReadAllBytes(filename)
Response.ContentType = "application/octet-stream"
Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename)
Response.BinaryWrite(binFile)
Response.End()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
response.binarywrite()将整个文件缓冲到内存,您可以使用httpresponse.transmitfile(),该文件不会在内存中缓冲文件。
更好的解决方案是将文件发送为块,流和...
这是一个无需缓冲>的下载文件的示例
Response.BinaryWrite() buffers whole file into memory, you can Use HttpResponse.TransmitFile() which don't buffer the file in memory.
Better solution is to send file as chunks, stream and ...
Here is a sample for download file without buffer