File upload is simple in Dotnet


HttpPostedFile postedFile		= Request.Files["fieldname"];
if (posterFile != null)
    fileUpload.SaveAs(filePath);

Ask the request object for an objectfied uploaded file and use its methods to save the file to the relevent location. Can not be any simpler.

The shocker first

For the above code to work Dotnet has to store the file somewhere so that when you ask for it you will get the file. You would think you can tell Dotnet a staging directory somewhere to accomplish this. But there is no such staging directory. Instead Dotnet stores the file in memory until you claim it or until the request completes. As a result it is wise not to upload very large files. To safeguard against such an eventuality Dotnet limits the default size to 4M.

The compromise

You can place the following xml entry in the web.config to change the default value


<httpRuntime 
            executionTimeout="90" // in secs
            maxRequestLength="4096" // in kilo bytes
/>

maxRequestLength determines how big of a file you can expect to be able to upload. When you increase this number, increase also the execution time out.

Additional precautions

There is some bearing on the session time out and also the process lock out time. See additional references for tuning these.

Alternatives

Use third party file upload programs that directly uses the ISAPI to accomplish this. Using these programs you can upload very very large files. You also can prompt the user with a progress bar etc.

>> Friday, July 02, 2004 9:55:28 AM - Comments by satya

The following configuration entries

httpRuntime
maxRequestLength
executionTimeout
sessionstateTimeout
responseDeadLockInterval

are discussed in detail at :

File upload configuration entries from ABUpload.net

>> Friday, July 02, 2004 9:59:09 AM - Comments by satya

MSDN Reference

The following reference at MSDN discusses the configuration parameters that are relevent to file upload.

Configuring file size limits in dotnet: httpruntime reference from msdn