Signup · Login
Stardeveloper.com  
Home · Articles · Forums · Advertise · Contact
Article Categories
.NET  .NET
  ASP (15)
  ASP.NET (26)
  ADO (16)
  ADO.NET (10)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Products (1)
  Applets (1)
  Patterns (1)
Latest Forum Activity
what is the right code to link the asp page t..
by amylisa on 22 Jul 2008 Go To Post

Can Loader.asp Get Form Elements
by azziham on 14 Jul 2008 Go To Post

Good asp resource sites
by codemylife on 3 Jul 2008 Go To Post

Re: Unable to insert data in an Access databa..
by asia on 3 Jul 2008 Go To Post

Re: problem with do while loop
by idsanjeev on 30 Jun 2008 Go To Post

Log In
UserName Or Email:

Password:

Auto-Login:

Miscellaneous Links
  Submit Article

Hosted by Securewebs.com
 
Home : .NET : ASP.NET : File Uploading in ASP.NET Using C#
 

File Uploading in ASP.NET Using C#
by Sriram Vaideeswaran.

Overview :
File uploading has always been a tedious task for the web developer community. Either we have to use a third party component or write several lines of code. With the advent of ASP.NET file uploading has become a lot easier, importantly without the need for third party components. All we have to do is to use the ASP.NET HTML File control and write a few lines of C# code.

This article guides you through the file uploading process. In classic ASP we would use the HTML file control for uploading files. In ASP.net we replace this with the ASP.NET HTML File control.The declaration of ASP.NET HTML file control is much like normal HTML file control except for the runat attribute set to server. (e.g)

<input type="file" id="myfile" name="myfile" runat="server" />

Similarly we use the ASP.NET button control which the the user will click to upload the file he has selected using the file control. As ASP.NET follows event based programming model we can attach a server side event which will respond to clicking of the button. This is done by using the OnClick event handler of the button control. To this event we assign the name of the method which we want to be executed on the server when the user clicks the button. (e.g)

<asp:button id="cmdUpload" name="cmdUpload" runat="server" OnClick="UploadFile" />

Now we can write C# code within the UploadFile method to save the uploaded file in the server. This code can be written in two ways depending on the number of file controls we have within the page. 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.

Properties
Property Description
FileName Returns the full path and name of the uploaded file.
ContentType The MIME type of the uploaded file.
ContentLength The size in bytes of the uploaded file.

Method
Methods Description
SaveAs(Path) Saves the uploaded file to the specified path.

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.


 ( No Further Pages )

Download Associated Files
002D.zip

Related Articles
  1. Displaying XML Data in an ASP.NET page : Binding DataGrid to an XML File
  2. Sending e-mail with attachments from an ASP.NET page
  3. Your first ASP.NET page
  4. Browser Capabilities Component in ASP.NET
  5. Sending E-Mails using ASP.NET
  6. A Preview of Active Server Pages+
  7. Exposing Web Services
  8. Get Detailed Information About Your Site Visitors In Real Time using ASP.NET
  9. ASP.NET Website Programming: Problem - Design - Solution : Deploying the Site
  10. How to determine what server is given web site running on using ASP.NET?

Comments/Questions ( Threads: 2, Comments: 4 )
    Contains 1 or more replies by the Author of this Article.
    Contains 1 or more replies by Faisal Khan.

  1. (PLEASE HELP) Error While Using this code in ASP.Net (File Uploading) ( 1 Reply )
  2. Great Tool! One minor snag? ( 1 Reply ) This thread contains 1 reply by Faisal Khan.

Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed.

 
© 1999 - 2008 Stardeveloper.com, All Rights Reserverd.