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

My ASP.NET application allows users to upload and download large files. Both procedures involve reading and writing files. What can I do to prevent the application from freezing and crashing when it is processing a large file? Should file operations be handled on a worker thread, for example?

+1


source to share


2 answers


Make sure you store your files correctly so that they don't take up too much memory on the system.

eg. excerpt from the download application, inside a 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 sane, eg. 100,000 bytes, the tradeoff is that it will be slower for smaller buffer sizes.

http://support.microsoft.com/kb/812406

Edit: Also make sure IIS is configured for a sufficiently large request length (IIS7) and timeout.

+1


source


If this is not the main purpose of your site, consider splitting these operations into a separate application, for example. sub-application or subdomain. In addition to reducing risk, it will also make it easier to scale as your user base grows.



0


source







All Articles