HTTP file upload in ASP.NET

21 May

While working on a project that accepts files from another system and stores them locally, I noticed there was not much information on how to do this not using default ASP.NET controls.

Here is a quick summary or quide with a few important tips you may need.

Let’s start from the beginning. To access files uploaded by a client on the server-side, say using PHP, you would use $_FILES collection. In ASP.NET this collection is called HttpFileCollection. Note, the file collection is populated only when the HTTP request Content-Type is “multipart/form-data”. To check Content-Type of the request run the following code:


//multipart/form-data; boundary=---------------------------7d9209171f0356 

Another thing you should remember is maximum file size restrictions. By default it’ set to 4096 KB (4 MB). It depends on the value of the MaxRequestLength configuration setting. If you attempt to upload a file that is longer than this value, the upload fails. To check the default maximum size for the current confuguration try the following:

using System.Web.Configuration;

HttpRuntimeSection configSection = new HttpRuntimeSection();

//Result: 4096

Now back to the file upload itself. As I already said HttpFileCollection will provide access to the files uploaded by a client.

HttpFileCollection Files = Request.Files;

In the example above I declared a variable of type HttpFileCollection that “holds” uploaded files.

The great thing about collection data type is that you can access it’s members either by name or by index (the same way as you do for arrays). Let’s take the previous example and get the length of each file in the Files collection:

HttpFileCollection Files = Request.Files;

string[] keys = Files.AllKeys;

for (int i = 0; i<keys.Length; i++) {
    Response.Write("File: " + keys&#91;i&#93; + " with ");
    Response.Write("size of " + Files&#91;i&#93;.ContentLength + "<br />");

In last example Files.AllKeys is a string array that contains the keys (names) of all members in the file collection.

Each individial file (member of HttpFileCollection) has HttpPostedFile type which provides properties and methods to get information about a file, to read and save it.

To check the type of the variable or object use GetType method of Type class.


//Result: System.Web.HttpPostedFile 

The example below is the function that checks file size and saves an uploaded file to disk.

Boolean SaveFile(HttpPostedFile fl,string fieldname,string refNum) {
        if ((fl != null) && (fl.ContentLength > 0)) {
            String currentDir = Server.MapPath("Uploaded") + "\\clientDocuments\\";
            int fileLen = fl.ContentLength;
            if (fileLen > 512000) return false;
            byte[] buf = new byte[fileLen];
            fl.InputStream.Read(buf, 0, fileLen);
            string[] spl = fl.FileName.Split('.');
            string shortfname = rnd.Next().ToString() + "." + spl[spl.Length - 1];
            string fname = currentDir + shortfname;
            FileStream fr = new FileStream(fname, FileMode.Create);
            fr.Write(buf, 0, buf.Length);
            return true;
        return false;

Hope you find it useful.

Leave a comment

Posted by on May 21, 2009 in ASP.NET


Tags: , , ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: