File Uploading in asp.Net using C#

In this tutorial you will learn how to upload file in using c#. Nowadays it is a common requirement in almost all dynamic websites to allow users to upload file . In this tutorial we will look how to upload single file. Let's have a look over how to do so

file uploading in

<asp:Label ID="lblMessage" runat="server" Visible="false"></asp:Label> </td>
<strong>Upload File</strong>
<asp:FileUpload ID="txtFile" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click"/>

As you have seen that we just have asp Label, asp FileUpload and asp Button Controls in our .aspx page. By default I have set the visible property of asp:Label to false that will display message, it will be automatically set to true whenever the file will be successfully uploaded or failed.

Now let's have a look over c# code behind. Right now I am just giving the facility to upload the .pdf file but you can allow user to upload any file you want by customizing this code. using the OnClick event handler of the asp button i am uploading the file to the server.
Note:- FileUpload server control is first introduced by ASP.NET 2.0

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class Upload_single_file : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)



//Function to upload the fileprotected void btnUpload_Click(object sender, EventArgs e)

//Checking whether asp file upload control contains any file or not
//If it contains file
if (txtFile.HasFile)
//Getting file name
string fileName = txtFile.FileName.ToString();

//Creating array of dots name that will contain the data before and after period (.)
string[] dots = fileName.Split('.');

string fileType = "pdf";
//type = dots[2 - 1] // type=dots[1] which contains your file extension

string type = dots[dots.Length - 1];
//If uploaded file is not in pdf format then this set of code will be executed
if (fileType.IndexOf(type) == -1)
lblMessage.Text = "Please only upload files that end in types: \n\n" + (fileType) + "\n\nPlease select a new file and try again.";
//If uploaded file is in pdf format then we are uploading that file to the server.
string strUploadPath = "", strFilePath = "";

//Replace your folder Name with this Uploaded Folder Name
strFilePath = @"~\uploaded Folder Name\";

//Current Date Time is appending with the File Name to track when the file
//is uploaded
string path = DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Year.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Millisecond.ToString();
strUploadPath = Server.MapPath(strFilePath) + path + txtFile.FileName.ToString().Replace("'", "");

lblMessage.Text = "File Uploaded Successfully";

I am performing server side validation to check whether the file submitted to be upload is .pdf or not but later on I will write a post related Regular Expressions in and in that post I will tell you how useful regular expressions are and also I will list the most commonly used Regular Expressions so that you just have to copy/paste them and they will solve your biggest problem with minimum code.

Now lets understand the code. I have used the SaveAs method of FileUpload Control that takes the full path of the folder where the file will be uploaded. Make sure that you must have write permissions on that folder.

How to work around the File Size Limitations:-

Probably you may not believe but it is truth that there is a limit to the file upload size. By default the maximum size of a file that you can upload using the FileUpload control is around about 4MB.

So if you want to upload the file greater than the 4MB then you have to edit the default settings.

To change the file size limit, you have to perform the changes in either web.config.comments (found in the ASP.NET 2.0 configuration folder at C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG) or in the web.config file of your application.

Now open your web.config.comments file, find a node that is <httpRuntime>, looks like the following:

apartmentThreading="false" />

There are lot of settings inside <httpRuntime> but our concern is only with maxRequestLength attribute. By default the value of maxRequestLength is set to 4096 KB, simply you have to change this value to your own value that will become the limit to the size of the file needed to be uploaded.

If you want to allow users to upload 10 MB files to the server, set the maxRequestLength value to 11264, it means that the application will allow the files that are up to 11000 KB to be uploaded to the server.

One more setting involved in the size limitation of files needed to be uploaded is the value given to the executionTimeout attribute in the <httpRuntime> node.
The value provided to the executionTimeout attribute is the number of seconds the upload process is allowed to occur before it is being shut down by ASP.NET. If you want to allow large files to be uploaded to the server, you should also increase this value along with the maxRequestLength value.

If you perform any change related limitation of file size in the web.config.comments file then that setting change will be applies to all the applications on the server.

If you want to apply file size setting change to the application you are working with then apply this node to the web.config file of your application and rollback all the changes that you have performed in the web.config.comments. Make sure this node should be resided between the <system.web> nodes in the web.config file.

Right now according to the code written, the file will be uploaded to the folder placed in the root directory.

So this is the way to upload the file in using c#.

Happy Coding !!!