In the following blog post, you will learn what is file upload control and how to implement file upload control in asp.net c# with examples. The FileUpload control was introduced in ASP.NET 2.0. Keep reading on How to Create a Calendar Control in ASP.NET.
What is file upload control in ASP.Net?
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.
Properties
- FileBytes
- FileContent
- FileName
- SaveAs
- PostedFile
The FileUpload control provides the HttpPostedFile class. Its has the following properties:
- ContentLength
- ContentType
- FileName
- InputStream
- SaveAs
File Upload Control in ASP.NET C# with Example
The following example shows how to use an ASP.NET FileUpload control to browse files and upload them to the Web server. Let’s get started by creating a new web application using the visual studio.
- You may also like How to Upload File in ASP.NET Core MVC
To create a control, simply drop the FileUpload control from Toolbox to a Web page. The following code adds FileUpLoad control:
<asp:FileUpload ID="fileUpload1" runat="server" />
To support file upload, we need to add a Link Button control:
<asp:LinkButton ID="OnLnkUpload" runat="server" OnClick="OnLnkUpload_Click">Upload</asp:LinkButton></td>
Now on this button click event handler, we need to call the SaveAs method of FileUpLoad control, which takes a full path where the file will be uploaded. Find the below full source code:-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Upload and Download files from Folder (Directory) in ASP.Net using C#</title> </head> <body> <form id="form1" runat="server"> <h2>File Upload and Download in ASP.NET C#</h2> <div> <table style="padding: 20px; font-family: Verdana, Geneva, Tahoma, sans-serif; font-size: medium; border: 1px solid black;" width="60%" cellpadding="25"> <tr> <td> <asp:Label ID="lblFilename" runat="server" Text="Browse:"></asp:Label> </td> <td> <asp:FileUpload ID="fileUpload1" runat="server" /> </td> <td> <asp:LinkButton ID="OnLnkUpload" runat="server" OnClick="OnLnkUpload_Click" Font-Underline="False" Font-Size="11pt">Upload</asp:LinkButton></td> <td> <asp:LinkButton ID="OnLnkDownload" runat="server" OnClick="OnLnkDownload_Click" Font-Underline="False" Font-Size="11pt">Download</asp:LinkButton></td> </tr> <tr> <td colspan="4"> <asp:Label ID="lblMessage" runat="server" ForeColor="#009900"></asp:Label></td> </tr> </table> </div> </form> </body> </html>
using System; using System.IO; public partial class _Default : System.Web.UI.Page { string filename = string.Empty; protected void Page_Load(object sender, EventArgs e) { } //To upload file protected void OnLnkUpload_Click(object sender, EventArgs e) { filename = Path.GetFileName(fileUpload1.PostedFile.FileName); fileUpload1.SaveAs(Server.MapPath("Files/" + filename)); lblFilename.Text = "Files/" + fileUpload1.FileName; lblMessage.Text = "File uploaded sucessfully to the folder: -" + " Files/" + fileUpload1.FileName; } // To download uplaoded file protected void OnLnkDownload_Click(object sender, EventArgs e) { if (lblFilename.Text != string.Empty) { if (lblFilename.Text.EndsWith(".txt")) { Response.ContentType = "application/txt"; } else if (lblFilename.Text.EndsWith(".pdf")) { Response.ContentType = "application/pdf"; } else if (lblFilename.Text.EndsWith(".docx")) { Response.ContentType = "application/docx"; } else { Response.ContentType = "image/jpg"; } string filePath = lblFilename.Text; Response.AddHeader("Content-Disposition", "attachment;filename=\"" + filePath + "\""); Response.TransmitFile(Server.MapPath(filePath)); Response.End(); } } }
Note: In the same way you can upload multiple files in ASP.NET by setting the AllowMultiple=” true” property as shown below:-
<asp:FileUpload ID="fileUpload1" AllowMultiple=" true" runat="server" />
Conclusion
I hope you liked this article on the file upload control in asp.net. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.
Provide a full functional project instead of just the source code only !
Thanks for your suggestion. I have uploaded all of the fully functional projects and you can find them here: https://dotnettec.com/shop/