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. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title></title>
  6. </head>
  7. <body>
  8. <form id="form1" runat="server">
  9. <div>
  10. </div>
  11. <asp:FileUpload ID="FileUpload1" runat="server" BackColor="#FF66CC" Font-Size="Medium" Height="38px" Width="301px" />
  12. <p>
  13. <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" />
  14. <asp:Label ID="Label1" runat="server" BackColor="Lime" Font-Size="Medium" Text="Label"></asp:Label>
  15. </p>
  16. </form>
  17. </body>
  18. </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. namespace FileUploadapplication
  8. {
  9. public partial class WebForm1 : System.Web.UI.Page
  10. {
  11. protected void Page_Load(object sender, EventArgs e)
  12. {
  13. }
  14. protected void Button1_Click(object sender, EventArgs e)
  15. {
  16. if (FileUpload1.HasFile) //fileupload control contains a file
  17. try
  18. {
  19. FileUpload1.SaveAs("E:\\" + FileUpload1.FileName); // file path where you want to upload
  20. Label1.Text = "File Uploaded Sucessfully !! " + FileUpload1.PostedFile.ContentLength + "mb"; // get the size of the uploaded file
  21. }
  22. catch (Exception ex)
  23. {
  24. Label1.Text = "File Not Uploaded!!" + ex.Message.ToString();
  25. }
  26. else
  27. {
  28. Label1.Text = "Please Select File and Upload Again";
  29. }
  30. }
  31. }
  32. }
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.