| |   Skin:   
      User: Not logged in 
Newsletter Signup
XSLT Library
Latest XML Blogs
Featured Examples
Featured Articles
Book Chapters
Training Courses
Sign In
My Profile
My Articles
My Examples
My Favorites
My Resources
Add a Resource
About Me
My Blog
HeadGeek Articles
Talking Portfolio
World Trip Pics

Uploading Files
Author: phanindra yerra
Published: 6/13/2006 7:31 AM
Category: C#
Summary: This article explains uploading files


Namespaces System.IO




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.


Screen Shot




  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.


                        if (_fileSize<=0)

                              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


      int IntLoop=0;


                  //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);


                        if (file.Exists)






                              Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);


                              Response.AddHeader("Content-Length", file.Length.ToString());


                              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.
































Fans of "The Office"
Dwight Bobbleheads are here!
  “It's me! I'm the bobblehead! Yes!”

Advertise on XMLPitstop

Advertise on XMLPitstop

EggHead Cafe
Web Servicee development
Buy text ads here!
Color Laser Printer
online business degree
Versace sunglasses
Skype vs. sipcall
VoIP Internettelefonie
Buy text ads here!

Interested in Text ads?
2,219 Total Members
34 members(last 30 days)
7 members(last 7 days)
1 members(today)

1,609 Total Discussions
14 Posts(last 30 days)
0 Posts(last 7 days)
0 Posts(today)

17,106 Total Blog Posts
1,791 Blogs(last 30 days)
273 Blogs(last 7 days)
44 Blogs(today)

8,699 Newsgroup Posts
0 Posts(last 30 days)
0 Posts(last 7 days)
0 Posts(today)

13,785 Total Resources
4 Resources(last 30 days)
1 Resources(last 7 days)
0 Resources(today)


David Silverlight's| 2801 Florida Ave #225|Miami, FL 33133|Ph:305-447-1139