When handling large file transfers in ASP.NET what precautions should you take?

依然范特西╮ 提交于 2019-12-11 07:05:42

问题


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?


回答1:


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:

// Read the data in buffer.
length = iStream.Read(buffer, 0, bufferSize);

// Write the data to the current output stream.
Response.OutputStream.Write(buffer, 0, length);

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.




回答2:


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.



来源:https://stackoverflow.com/questions/276088/when-handling-large-file-transfers-in-asp-net-what-precautions-should-you-take

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!