Using ASP.NET FileUpload Control

ASP.NET FileUpload control allows us to upload files to a Web Server or storage in a Web Form. The control is a part of ASP.NET controls and can be placed to a Web Form by simply dragging and dropping from Toolbox to a WebForm. The FileUpload control was introduced in ASP.NET 2.0.

This article and code example shows how to use an ASP.NET FileUpload control to browse files and upload them to the Web server.
 
Create an ASP.NET Web application using Visual Studio.  

To create a control, simply drop the FileUpload control from Toolbox to a Web page. The following code adds the FileUpLoad control:

  1. <asp:FileUpload id="FileUpLoad1" runat="server" />  
To support file upload, we need to add a Button control:
  1. <asp:Button id="UploadBtn" Text="Upload File" OnClick="UploadBtn_Click" runat="server" Width="105px" />  
Now on this button click event handler, we need to call SaveAs method of FileUpLoad control, which takes a full path where the file will be uploaded.
  1. protected void UploadBtn_Click(object sender, EventArgs e)  
  2. {  
  3.     if (FileUpLoad1.HasFile)  
  4.     {  
  5.   
  6.         FileUpLoad1.SaveAs(@"C:\temp\" + FileUpLoad1.FileName);  
  7.         Label1.Text = "File Uploaded: " + FileUpLoad1.FileName;  
  8.     }  
  9.     else  
  10.     {  
  11.         Label1.Text = "No File Uploaded.";  
  12.     }  
  13. }  
The final page looks like Figure 1.

Figure 1. FileUpLoad control

Now if you run the sample, browse a file, and click on Upload File button, the output looks like Figure 2.

Figure 2. FileUpLoad control in action

Restricting File Types

You can restrict the FileUpload control to upload a file type. For example, I wanted to upload images (Jpgs and Gifs) only so I put a validator, which allows Gifs and Jpegs only.

  1. <asp:RegularExpressionValidator   
  2. id="FileUpLoadValidator" runat="server"   
  3. ErrorMessage="Upload Jpegs and Gifs only."   
  4. ValidationExpression="^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))(.jpg|.JPG|.gif|.GIF)$"   
  5. ControlToValidate="FileUpload1">  
  6. </asp:RegularExpressionValidator> 

Figure 3. Validating images only.

I was trying to upload a file with over 10MB and was getting error and this is why?

The default size of files uploaded by the FileUpload control is 4MB. So if you try to upload the files larger than 4MB, it won't let you do so. To do so, you need to change the default file size in machine.config.comments file for maxRequestLength of httpRuntime tag.  This number is in KB.

  1. <httpRuntime  
  2. executionTimeout = "110" [in Seconds][number  
  3. maxRequestLength = "4096" [number]  
  4. requestLengthDiskThreshold = "80" [number]  
  5. useFullyQualifiedRedirectUrl = "false" [true|false]  
  6. minFreeThreads = "8" [number]  
  7. minLocalRequestFreeThreads = "4" [number]  
  8. appRequestQueueLimit = "5000" [number]  
  9. enableKernelOutputCache = "true" [true|false]  
  10. enableVersionHeader = "true" [true|false]  
  11. apartmentThreading = "false" [true|false]  
  12. requireRootedSaveAsPath = "true" [true|false]  
  13. enable = "true" [true|false]  
  14. sendCacheControlHeader = "true" [true|false]  
  15. shutdownTimeout = "90" [in Seconds][number]  
  16. delayNotificationTimeout = "5" [in Seconds][number]  
  17. waitChangeNotification = "0" [number]  
  18. maxWaitChangeNotification = "0" [number]  
  19. enableHeaderChecking = "true" [true|false]  
  20. /> 
In this code sample, we saw how to use a FileUpload control in ASP.NET form to upload files to a web server. 
 
Here is a detailed tutorial: ASP.NET MVC Form with File Upload that includes step by step procedure of creating a form, uploading files to a Web server and storing files in a database.