With the ASP.NET file uploading has become an easier without any third party tools . By using html file control and writing a few lines of c# code is enough. This article provides you code for uploading and downloading the files.
If there is only a single file control then we can use the PostedFile property of the file control to upload files. This property is of type HttpPostedFile class and contains the following methods and properties which aids file uploading.
FileName -- Returns the full path and name of the uploaded file.
ContentType -- Returns the full path and name of the uploaded file
ContentLength -- The size in bytes of the uploaded file.
Methods used are SaveAs(Path) Saves the uploaded file to the specified path.
First paste this inside form tag for file field
<INPUT id="file" style="Z-INDEX: 102; LEFT: 64px; POSITION: absolute; TOP: 112px" type="file" runat="server">
next declare that html control field in your webform class as
protected System.Web.UI.HtmlControls.HtmlInputFile file;
in upload button click event handling function write this code
if (file.PostedFile !=null) //Checking for valid file
// FileName property gives the entire path but we need only file name so here substring is used.
string _fileName = file.PostedFile.FileName.Substring(file.PostedFile.FileName.LastIndexOf("\\") + 1) ;
string _fileType = file.PostedFile.ContentType ;
int _fileSize = file.PostedFile.ContentLength;
//Checking for the length of the file. If length is 0 then file is not uploaded.
Response.Write("File Uploading failed for" + _fileName );
file.PostedFile.SaveAs(Server.MapPath(".\\" + _fileName));
Response.Write( "Your File " + _fileName + " of Type " + _fileType + " and size " + _fileSize.ToString() + " bytes was uploaded successfully");
If we have multiple file controls within the form we can use the Files property of the Request object which returns a reference to the HttpFileCollection class. HttpFileCollection class has an Item property through which gets individual HttpPostedFile from the file collection either by specifying the name or index.
As shown in below screen shot
in update event handle write this code
//Iterating through the Request.Files collection
if (Request.Files[IntLoop] !=null) //Checking for valid file
// Since the FileName gives the entire path we use Substring function to rip of the filename.
string StrFileName = Request.Files[IntLoop].FileName.Substring(Request.Files[IntLoop].FileName.LastIndexOf("\\") + 1) ;
string StrFileType = Request.Files[IntLoop].ContentType ;
int IntFileSize = Request.Files[IntLoop].ContentLength;
//Checking for the file length. If length is 0 then file is not uploaded.
if (IntFileSize <=0)
Response.Write(" <font color='Red' size='2'>Uploading of file " + StrFileName + " failed. </font><br>");
//Saving the file to the web server
Request.Files[IntLoop].SaveAs(Server.MapPath(".\\" + StrFileName));
Response.Write( "<font color='green' size='2'>Your file " + StrFileName + " of type " + StrFileType + " and size " + IntFileSize.ToString() + " was uploaded successfully.</font><br>");
Downloading file from server
Downloading is also very easy in asp .net just write code like this
string filename = TextBox1.Text;
if (filename != "")
string path = Server.MapPath(filename);
System.IO.FileInfo file = new System.IO.FileInfo(path);
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.ContentType = "application/octet-stream";
Response.Write("This file does not exist.");
This article explained how to download and upload files from and to the server respectively.