在 ASP.NET 中处理大文件传输时应采取哪些预防措施?
我的 ASP.NET 应用程序允许用户上传和下载大文件。 这两个过程都涉及读取和写入文件流。 我应该如何确保应用程序在处理大文件时不会挂起或崩溃? 例如,文件操作是否应该在工作线程上处理?
My ASP.NET application allows users to upload and download large files. Both procedures involve reading and writing filestreams. What should I do to ensure the application doesn't hang or crash when it handles a large file? Should the file operations be handled on a worker thread for example?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
确保正确缓冲文件,以免它们占用系统中过多的内存。
例如,摘录自下载应用程序,在读取文件的 while 循环内:
其中 bufferSize 是合理的,例如 100000 字节,权衡是较小的缓冲区大小会更慢。
http://support.microsoft.com/kb/812406
编辑:还要确保 IIS设置为采用足够大的请求长度 (IIS7)和超时。
Make sure you properly buffer the files so that they don't take inordinate amounts of memory in the system.
e.g. excerpt from a download application, inside the while loop that reads the file:
Where bufferSize is something reasonable, e.g. 100000 bytes, the trade-off is that it will be slower for smaller buffer sizes.
http://support.microsoft.com/kb/812406
Edit: Also be sure that IIS is set to take a large enough request length (IIS7) and timeout.
除非这是您站点的主要目的,否则请考虑将这些操作分区到单独的应用程序,例如子应用程序或子域。 除了降低风险之外,这还可以随着用户群的增长而简化扩展。
Unless this is the primary purpose of your site consider partitioning these operations to a separate application, e.g. a sub-application or sub-domain. Besides reducing risk this would also simplify scaling out as your user base grows.