fileupload control in asp.net csharp

What is a FileUpload control in asp.net csharp -: This control is use for uploading a file. image, or any video.We can insert any file in our web server using the Asp.Net FileUpload control that allows us to upload files to a Web Server or storage in a Web Form. FileUpload control is a part of Asp.net controls.This control was introduced in Asp.net 2.0.

How to insert FileUpload Control -: Like any other control we can insert this control using the tool box or source code.For inserting the file control in our web page or window form go to the tool box and select the FileUpload control.Now double click this control or drag and drop this control.For example

<asp:FileUpload ID="FileUpload1" runat="server" />

Property of FileUpload control -: Some property are the same of label control or text box control.For example we can set the format as like back color, border color,font etc.Some special property has been given here.

AllowMultiple -: When we click this property its asked the two condition one is true and second is false.If we want to select one file upload the select the false condition. If we want to upload multiple file then select True.

How to Upload a file on Server

Uploading a file on the server we need a FileUpload control, label and Button control. Now we design the source code in the fileupload.aspx page here as like

 <div>
        <asp:FileUpload ID="FileUpload1" runat="server"  Font-Bold="True" Font-Italic="True" Font-Size="Large" />
        <br />
        
   
        <asp:Button ID="Button1" runat="server" BackColor="#66FFFF" Font-Bold="True" Font-Italic="True" ForeColor="Fuchsia" OnClick="Button1_Click" Text="Upload" />

        <asp:Label ID="Label1" runat="server" Text="Label" Visible="False"></asp:Label>
    </div>

Now we write the programming in fileupload.aspx.cs page here as like

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class fileupload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            try
            {
                string filename = Path.GetFileName(FileUpload1.FileName);
                FileUpload1.SaveAs(Server.MapPath("~/") + filename);
                Label1.Text = "Upload status: File uploaded!";
                Label1.Visible = true;
            }
            catch (Exception ex)
            {
                Label1.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
                Label1.Visible = true;
            }
        }
    }
}

Now Run the program. Now select the file which you want to upload on server and click the upload button.

file upload control in asp.net c#

Example of Multiple file upload in asp.net c#

Now we will see the example of multiple file uploading on server. For uploading a multiple file we set the property of FileUpload control. We select the property AllowMultiple and select true of FileUpload control as like

<asp:FileUpload ID="FileUpload1" runat="server" AllowMultiple="True" Font-Bold="True" Font-Italic="True" Font-Size="Large" />

For uploading multiple file we take a fileupload control,label and button control in fileupload.aspx page as like

<div>
        <asp:FileUpload ID="FileUpload1" runat="server" AllowMultiple="True"  Font-Bold="True" Font-Italic="True" Font-Size="Large" />
        <br />
        
   
        <asp:Button ID="Button1" runat="server" BackColor="#66FFFF" Font-Bold="True" Font-Italic="True" ForeColor="Fuchsia" OnClick="Button1_Click" Text="Upload" />

        <asp:Label ID="Label1" runat="server" Text="Label" Visible="False"></asp:Label>
    </div>

Now we create a programming in fileupload.aspx.cs page here as like

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class fileupload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
  protected void Button1_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            foreach (HttpPostedFile uploadedFile in FileUpload1.PostedFiles)
            {

                uploadedFile.SaveAs(System.IO.Path.Combine(Server.MapPath("~/upload/"), uploadedFile.FileName));

                Label1.Text += String.Format("{0}<br />", uploadedFile.FileName);
                Label1.Visible = true;
                             
            }
        }

    }
}

Now run the program and select multiple file here.all the file store in the upload folder.Now create a folder on server and give the name folder upload. if you want to store different name or store to direct you can remove your folder name here.

Use of fileupload control in asp.net csharp -: We can upload any file, any image,any video on server without opening a hosting file. There are some dynamic website that asked the information and data then we can use this control.

Leave a Comment