当ASP.NET处理大型文件传输,你应该采取什么样的precautions? [英] When handling large file transfers in ASP.NET what precautions should you take?
问题描述
我的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?
推荐答案
请确保你正确地缓冲文件,以便它们不采取过多数量的内存在系统中。
Make sure you properly buffer the files so that they don't take inordinate amounts of memory in the system.
例如。从下载的应用程序,while循环中,读取文件摘录:
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);
在哪里缓冲区大小是合理的东西,例如, 100000字节,权衡的是,这将是更小的缓冲区大小慢。
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
编辑:另外要确保IIS设置取一个足够大的请求长度(IIS7),并暂停。
Also be sure that IIS is set to take a large enough request length (IIS7) and timeout.
这篇关于当ASP.NET处理大型文件传输,你应该采取什么样的precautions?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!