ASP.NET File Upload control

  • by
ASP.NET File Upload example -

ASP.NET File upload control :

.NET file upload control makes it easier to upload files on website. To use file upload control in your project, use following design code :

<div style="padding: 2px; font-family: Arial; font-weight: bold;">
 <h3>Add Files</h3>

Select a file and click Upload
 <br />
 <br />

<asp:FileUpload runat="server" ID="fileUpload1" />
 <br />
 <br />
 <asp:Button runat="server" Text="upload" ID="btnUpload"
 OnClick="btnUpload_Click" />
 <br />
 <br />
 <asp:Label runat="server" ID="lblInfo" />
 </div>

The above lines of code, will make a file upload control, a asp.net button and a asp.net label. It will look like below image :

ASP.NET File Upload control -

ASP.NET File Upload control – Design Page layout

And to accept the files, you will have to use below code :

protected void btnUpload_Click(object sender, EventArgs e)
 {
 try
 {
 fileUpload1.SaveAs(Server.MapPath("~/Files/") + Path.GetFileName(fileUpload1.FileName));
 lblInfo.Text = "Your file was successfully uploaded to : Files/" + Path.GetFileName(fileUpload1.FileName).ToString();
 }
 catch (Exception ex)
 {
 lblInfo.Text = ex.Message.ToString();
 }
 }

In your project, create a folder named Files in which this files will be uploaded and copied.

ASP.NET File Upload control - Browser

ASP.NET File Upload control – Browser

 

ASP.NET file upload control can be used to upload any kinds of files and any size of file. However, by default the file size is limited. You can change it according to your preference and requirements.

You can also make this control to accept only specific types of file, for example a profile pic image will be png or jpg image format. Or a document module must accept only document formats (.csv, .txt, .xls, .xlsx, .pdf).

Below code will accept only PDF file formats :

protected void btnUpload_Click(object sender, EventArgs e)
 {
 try
 {
 if (fileUpload1.PostedFile.ContentType == "application/pdf")
 {
 fileUpload1.SaveAs(Server.MapPath("~/Files/") + Path.GetFileName(fileUpload1.FileName));
 lblInfo.Text = "Your file was successfully uploaded to : Files/" + Path.GetFileName(fileUpload1.FileName).ToString();
 }
 else
 {
 lblInfo.Text = "Only Pdf file is allowed.";
 }
 }
 catch (Exception ex)
 {
 lblInfo.Text = ex.Message.ToString();
 }
 }

To set the size limit use below code :

try
 {
 if (fileUpload1.PostedFile.ContentLength < 1024*500)
 {
 fileUpload1.SaveAs(Server.MapPath("~/Files/") + Path.GetFileName(fileUpload1.FileName));
 lblInfo.Text = "Your file was successfully uploaded to : Files/" + Path.GetFileName(fileUpload1.FileName).ToString();
 }
 else
 {
 lblInfo.Text = "maximum file size limit is 500kb";
 }
 }
 catch (Exception ex)
 {
 lblInfo.Text = ex.Message.ToString();
 }

This will accept only file size upto 500kb. In short, ASP.NET file upload control has many features.

 

 


  1. Base64Encode.io - Realtime Encode your string to Base64 format.
  2. Base64Decode.io - Realtime Decode Base64 data to plain text.
  3. Md5Hash.io - Realtime Encoding in Md5Hash.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.