November 21, 2010

The asp.net FileUpload control allows a user to browse and upload files to the web server. From developers perspective, it is as simple as dragging and dropping the FileUpload control to the aspx page. An extra control, like a Button control, or some other control is needed, to actually save the file.

aspx page
  1. <asp:FileUpload ID="FileUpload1" runat="server" />
  2. <asp:Button ID="B1" runat="server" Text="Save" OnClick="B1_Click" />

By default, the FileUpload control allows a maximum of 4MB file to be uploaded and the execution timeout is 110 seconds. These properties can be changed from within the web.config file’s httpRuntime section. The maxRequestLength property determines the maximum file size that can be uploaded. The executionTimeout property determines the maximum time for execution.

web.config file
  1. <httpRuntime maxRequestLength="8192" executionTimeout="220"/>

From code behind, the mime type, size of the file, file name and the extension of the file can be obtained. The maximum file size that can be uploaded can be obtained and modified using the System.Web.Configuration.HttpRuntimeSection class.

Files can be alternatively saved using the System.IO.HttpFileCollection class. This collection class can be populated using the Request.Files property. The collection contains HttpPostedFile class which contains a reference to the class.

Code behind
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.IO;
  8. using System.Configuration;
  9. using System.Web.Configuration;
  10.  
  11. namespace WebApplication1
  12. {
  13.     public partial class WebControls : System.Web.UI.Page
  14.     {
  15.         protected void Page_Load(object sender, EventArgs e)
  16.         {
  17.         }
  18.  
  19.         //Using FileUpload control to upload and save files
  20.         protected void B1_Click(object sender, EventArgs e)
  21.         {
  22.             if (FileUpload1.HasFile && FileUpload1.PostedFile.ContentLength > 0)
  23.             {
  24.                 //mime type of the uploaded file
  25.                 string mimeType = FileUpload1.PostedFile.ContentType;
  26.  
  27.                 //size of the uploaded file
  28.                 int size = FileUpload1.PostedFile.ContentLength; // bytes
  29.  
  30.                 //extension of the uploaded file
  31.                 string extension = System.IO.Path.GetExtension(FileUpload1.FileName);
  32.                 
  33.                 //save file
  34.                 string path = Server.MapPath("path");                
  35.                 FileUpload1.SaveAs(path + FileUpload1.FileName);
  36.                 
  37.             }
  38.             //maximum file size allowed
  39.             HttpRuntimeSection rt = new HttpRuntimeSection();
  40.             rt.MaxRequestLength = rt.MaxRequestLength * 2;
  41.             int length = rt.MaxRequestLength;
  42.        
  43.             //execution timeout
  44.             TimeSpan ts = rt.ExecutionTimeout;
  45.             double secomds = ts.TotalSeconds;
  46.  
  47.         }
  48.  
  49.         //Using Request.Files to save files
  50.         private void AltSaveFile()
  51.         {
  52.             HttpFileCollection coll = Request.Files;
  53.             for (int i = 0; i < coll.Count; i++)
  54.             {
  55.                 HttpPostedFile file = coll[i];
  56.  
  57.                 if (file.ContentLength > 0)
  58.                     ;//do something
  59.             }
  60.         }
  61.     }
  62. }

0 comments:

Reference: Shahed Kazi at AspNetify.com