Wednesday, 29 January 2014

ASP.NET - File Uploading

ASP.NET - File Uploading

ASP.Net has two controls that allow the users to upload files to the web server. Once the server receives the posted file data, the application can save it, check it or ignore it. The following controls allow the file uploading:
  • HtmlInputFile - an HTML server control
  • FileUpload - and ASP.Net web control
Both the controls allow file uploading, but the FileUpload control automatically sets the encoding of the form, whereas the HtmlInputFile does not do so.
In this tutorial, we will use the FileUpload control. The FileUpload control allows the user to browse for and select the file to be uploaded, providing a Browse button and a text box for entering the filename.
Once, the user has entered the filename in the text box, by typing the name or browsing, the SaveAs method of the FileUpload control can be called to save the file to the disk.
The basic syntax for using the FileUpload is:
<asp:FileUpload ID= "Uploader" runat = "server" />
The FileUpload class is derived from the WebControl class, and inherits all its members. Apart from those, the FileUpload class has the following read-only properties:
PropertiesDescription
FileBytesReturns an array of the bytes in a file to be uploaded..
FileContentReturns the stream object pointing to the file to be uploaded.
FileNameReturns the name of the file to be uploaded.
HasFileSpecifies whether the control has a file to upload.
PostedFileReturns a reference to the uploaded file.
The posted file is encapsulated in an object of type HttpPostedFile, which could be accessed through the PostedFile property of the FileUpload class.
The HttpPostedFile class has the following important properties, which are much used:
PropertiesDescription
ContentLengthReturns the size of the uploaded file in bytes.
ContentTypeReturns the MIME type of the uploaded file
FileNameReturns the full filename.
InputStreamReturns a stream object pointing to the uploaded file.

Example:

The following example demonstrates the FileUpload control and its properties. The form has a FileUpload control along with a save button and a label control for displaying the file name, file type and file length.
In the design view, the form looks like:
File Upload
The content file:
<body>
<form id="form1" runat="server">
<div>
<h3> File Upload:</h3>
<br />
   <asp:FileUpload ID="FileUpload1" runat="server" />
   <br /><br />
   <asp:Button ID="btnsave" runat="server" 
               onclick="btnsave_Click" Text="Save" 
               style="width:85px" />
   <br /><br />
   <asp:Label ID="lblmessage" runat="server" />
</div>
</form>
</body>
The code behind the save button:
protected void btnsave_Click(object sender, EventArgs e)
{
   StringBuilder sb = new StringBuilder();
   if (FileUpload1.HasFile)
   {
      try
      {
      sb.AppendFormat(" Uploading file: {0}", 
                                  FileUpload1.FileName);
      //saving the file
      FileUpload1.SaveAs("<c:\\SaveDirectory>" + 
                                  FileUpload1.FileName);
      //Showing the file information
      sb.AppendFormat("<br/> Save As: {0}", 
                         FileUpload1.PostedFile.FileName);
      sb.AppendFormat("<br/> File type: {0}",   
                         FileUpload1.PostedFile.ContentType);
      sb.AppendFormat("<br/> File length: {0}", 
                         FileUpload1.PostedFile.ContentLength);
      sb.AppendFormat("<br/> File name: {0}", 
                         FileUpload1.PostedFile.FileName);
      }
      catch (Exception ex)
      {
      sb.Append("<br/> Error <br/>");
      sb.AppendFormat("Unable to save file <br/> {0}", 
                         ex.Message);
      }
   }
   else
   {
      lblmessage.Text = sb.ToString();
   }
}
Note the following:
  • The StringBuilder class is derived from System.IO namespace, so it should be included.
  • The try and catch blocks are used for catching errors, and display the error message.

Related Posts:

  • ASP.NET - Client Side ASP.NET - Client Side ASP.Net client side coding has two aspects: Client side scripts: that would run on the browser and in turn would speed up the execution of page. For example, client side data validation whic… Read More
  • ASP.NET - ADO.net ASP.NET - ADO.net ADO.Net provides a bridge between the front end controls and the back end database. The ADO.Net objects encapsulate all the data access operations and the controls interact with these objects to disp… Read More
  • ASP.NET - Calendars ASP.NET - Calendars The calendar control is a functionally rich web control, which provides the following capabilities: Displaying one month at a time Selecting a day, a week or a month Selecting a range of days … Read More
  • ASP.NET - Ad Rotator ASP.NET - Ad Rotator The AdRotator control randomly selects banner graphics from a list, which is specified in an external XML schedule file. This external XML schedule file is called the advertisement file. The AdRo… Read More
  • ASP.NET - Basic Controls ASP.NET - Basic Controls In this section, we will discuss the basic controls available in ASP.NET Button Controls: ASP .Net provides three types of button controls: buttons, link buttons and image buttons. As the name… Read More

0 comments:

Post a Comment