As the move is made to more sophisticated internet applications, users expect more advanced features. An ability to upload files to the server may be necessary for a number of applications, including document management systems and content management systems.

In this article I’ll show you a simple way of allowing users to upload files to the server with ASP.NET applications.

Create code for a simple Web app
I’ll show you how to create a simple ASP.NET Web application that demonstrates uploading a file to a server. The C# code is available inListing A, and the HTML form is available in Listing B. But if you’d rather go hands-on, you can follow the steps I took to build this project.

First, you fire up Visual Studio .NET. On the Start Page, click on the New Project button. Choose the following:

  • Project Type: Visual C# Projects
  • Templates: ASP.NET Web Application
  • Location: http://localhost/FileUpload

Once the Web form opens up, click on Toolbox, select HTML section, find File Field control, and drag it onto the form. Right-click on the control and set Run As Server Control. Change the Name property of the control to File1.

Click on Toolbox again, select Web Forms section, find a Button, and drag it onto the form.Set its Text property to Upload and its ID property to cmdUpload. Your form should end up looking like Figure A.

Figure A
Creating the file upload form

Now add the following code to the form definition of the aspx file:

So that the full form tag looks like this:
<form id=”Form1″ method=”post” encType=”multipart/form-data” runat=”server”>

Add the following code to the declaration section of the cs file:
string sFileDir= “C:\\”;
long lMaxFileSize = 4096;

Note that the above values should be changed to the specific location and size limit that you want to set up. These values could also be retrieved either from a database, an INI file, or an XML file, but for this example you’re simply hard-coding them.

Add the following code to the very top portion of the cs page:
Add using System.IO;

Then, add the following procedure cs code page:
private void DeleteFile (string strFileName)
{//Delete file from the server
       if (strFileName.Trim().Length > 0)
       FileInfo fi = new FileInfo(strFileName);
              if (fi.Exists)//if file exists delete it

And finally, add the code in Listing C to the cs file.

Press [F5] to compile and run the project.

Specify a file that you want to upload by clicking Browse and choosing an actual file from your local hard drive. Your screen should look something like Figure B.

Figure B
Testing your file upload feature

Then click the Upload button on your form. You should see a message displayed on top, as in Figure C.

Figure C
Upload confirmation screen

You should check to see if the file has been copied to the directory specified in your code with its original file name.

How does this work?
Values sFileDir and lMaxFileSize are hard coded at the top and would normally be retrieved from a database or an external file so that they are easy to change. sFileDir specifies where on the server the copied files should be saved to lMaxFileSize specifies the maximum allowed file size for an uploaded file.

Procedure DeleteFile is used to delete the copied file from the server. After the file upload takes place, the file may be moved either to the database or to some other location on the server depending on the application requirements. In this example you don’t copy the file anywhere, so you don’t call this procedure unless an error occurs, and you need to get rid of it. DeleteFile can be called after the file has been moved into the database or to another location for cleaning purposes. It accepts a full file name (directory and file name) as one argument. It verifies that the file exists and that the length of the argument is bigger than 0 and attempts to delete the file through the use of FileInfo object.

When the user clicks the cmdUpload button, you first check if such a file exists. If the file exists you determine the file name without the directory (File1.PostedFile.FileName property stores the location and the name of the file on the client’s computer) using System.IO.Path.GetFileName. Then you check if the file size is not larger than the maximum allowed. You then save the file to the designated location on the server using File1.PostedFile.SaveAs method and pass the directory and the filename to it. Upon saving the file, you display a message stating that the file has been uploaded successfully. In case of an error, you delete the file and display the error message using lblMessage label.

When uploading the files, keep in mind that ASP.NET limits the file size of the uploaded files to 4 MB or 4096 KB. If you try to upload a file that is larger than that, you will get an error. You can change that setting by changing maxRequestLength setting in httpRuntime element of the Machine.config file.