File Upload Application In ASP.NET C#

Hello Friends. Hope you all are doing well. Today, I am sharing my article "File Upload Application ASP.NET C#." Thus, I hope it is helpful for you.

Step 1: You need to install VS in your machine (laptop & desktop). Open Visual Studio and start new project.

new project

Step 2: Select Web and select ASP.NET. Empty Web Application looks as given below. Enter your Application name and click OK.

application name

Step 3: Click Solution Explorer. Right click add new item and add OK. Design the page by dragging and dropping the uploaded file.

code

Design page is given below:

Design page

The source code of the design page is given below:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="FileUploadapplication.WebForm1" %>  
  2.   
  3. <!DOCTYPE html>  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.     <div>  
  12.       
  13.     </div>  
  14.         <asp:FileUpload ID="FileUpload1" runat="server" BackColor="#FF66CC" Font-Size="Medium" Height="38px" Width="301px" />  
  15.         <p>  
  16.             <asp:Button ID="Button1" runat="server" BackColor="Aqua" BorderStyle="Solid" Font-Bold="True" Font-Italic="False" Font-Size="X-Large" Height="48px" OnClick="Button1_Click" Text="Upload" Width="226px" />  
  17.             <asp:Label ID="Label1" runat="server" BackColor="Lime" Font-Size="Medium" Text="Label"></asp:Label>  
  18.         </p>  
  19.     </form>  
  20. </body>  
  21. </html>  
Afterwards, click the upload button and write the code, given below:
  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.   
  8. namespace FileUploadapplication  
  9. {  
  10.     public partial class WebForm1 : System.Web.UI.Page  
  11.     {  
  12.         protected void Page_Load(object sender, EventArgs e)  
  13.         {  
  14.   
  15.         }  
  16.   
  17.         protected void Button1_Click(object sender, EventArgs e)  
  18.         {  
  19.             if (FileUpload1.HasFile)  //fileupload control contains a file  
  20.                 try  
  21.                 {  
  22.                     FileUpload1.SaveAs("E:\\" + FileUpload1.FileName);          // file path where you want to upload  
  23.                     Label1.Text = "File Uploaded Sucessfully !! " + FileUpload1.PostedFile.ContentLength + "mb";     // get the size of the uploaded file  
  24.                 }  
  25.                 catch (Exception ex)  
  26.                 {  
  27.                     Label1.Text = "File Not Uploaded!!" + ex.Message.ToString();  
  28.                 }  
  29.             else  
  30.             {  
  31.                 Label1.Text = "Please Select File and Upload Again";  
  32.   
  33.             }  
  34.         }  
  35.     }  
Output of program

Output

When I click choose file it opens file, as given below. Select file and click upload.

Output

Keep reading. Share your feedback. Keep writing.Thanks.