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.
Step 2: Select Web and select ASP.NET. Empty Web Application looks as given below. Enter your Application name and click OK.
Step 3: Click Solution Explorer. Right click add new item and add OK. Design the page by dragging and dropping the uploaded file.
Design page is given below:
The source code of the design page is given below:
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="FileUploadapplication.WebForm1" %>
-
- <!DOCTYPE html>
-
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
-
- </div>
- <asp:FileUpload ID="FileUpload1" runat="server" BackColor="#FF66CC" Font-Size="Medium" Height="38px" Width="301px" />
- <p>
- <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" />
- <asp:Label ID="Label1" runat="server" BackColor="Lime" Font-Size="Medium" Text="Label"></asp:Label>
- </p>
- </form>
- </body>
- </html>
Afterwards, click the upload button and write the code, given below:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
-
- namespace FileUploadapplication
- {
- public partial class WebForm1 : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
-
- }
-
- protected void Button1_Click(object sender, EventArgs e)
- {
- if (FileUpload1.HasFile)
- try
- {
- FileUpload1.SaveAs("E:\\" + FileUpload1.FileName);
- Label1.Text = "File Uploaded Sucessfully !! " + FileUpload1.PostedFile.ContentLength + "mb";
- }
- catch (Exception ex)
- {
- Label1.Text = "File Not Uploaded!!" + ex.Message.ToString();
- }
- else
- {
- Label1.Text = "Please Select File and Upload Again";
-
- }
- }
- }
- }
Output of program
When I click choose file it opens file, as given below. Select file and click upload.
Keep reading. Share your feedback. Keep writing.Thanks.