aspnet
  1. aspnet-multiple-fileupload

Multiple FileUpload - ( ASP.NET Web Forms )

The Multiple FileUpload control in ASP.NET allows users to select and upload multiple files to the server at once. In this tutorial, we will discuss how to use the Multiple FileUpload control in ASP.NET Web Forms.

Syntax

The Multiple FileUpload control is included in the asp:FileUpload tag. It has the multiple attribute, which allows users to select multiple files for upload.

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

Example

Here is an example of using the Multiple FileUpload control in ASP.NET Web Forms.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UploadFiles.aspx.cs" Inherits="WebApplication1.UploadFiles" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:FileUpload ID="FileUpload1" runat="server" multiple />
            <br />
            <asp:Button ID="Button1" runat="server" Text="Upload" OnClick="Button1_Click" />
        </div>
    </form>
</body>
</html>

In the code-behind file, write the following code to handle the upload button click event.

protected void Button1_Click(object sender, EventArgs e)
{
    if (FileUpload1.HasFiles)
    {
        foreach (HttpPostedFile postedFile in FileUpload1.PostedFiles)
        {
            // save the file to the server
            postedFile.SaveAs(Server.MapPath("~/uploads/" + postedFile.FileName));
        }

        // show success message
        Label1.Text = "Files uploaded successfully.";
    }
    else
    {
        // show error message
        Label1.Text = "Please select at least one file to upload.";
    }
}

Output

After selecting and uploading files using the Multiple FileUpload control, the files will be saved to the server and a success message will be displayed.

Explanation

Using the Multiple FileUpload control in ASP.NET Web Forms allows users to select and upload multiple files to the server at once. The control is created by adding the multiple attribute to the asp:FileUpload tag.

In the code-behind file, the HttpPostedFile object is used to access each uploaded file and save it to the server. The foreach loop is used to loop through all the uploaded files.

Use

The Multiple FileUpload control is useful for allowing users to upload multiple files to the server at once. It is commonly used in web applications that require uploading of multiple files, such as image galleries, document libraries, and more.

Important Points

  • The Multiple FileUpload control is created by adding the multiple attribute to the asp:FileUpload tag.
  • The HttpPostedFile object is used to access each uploaded file and save it to the server.
  • The foreach loop is used to loop through all the uploaded files.

Summary

In this tutorial, we discussed how to use the Multiple FileUpload control in ASP.NET Web Forms. We covered the syntax, example, output, explanation, use, important points, and summary of the Multiple FileUpload control. By using this control, users can easily upload multiple files to the server in a single upload action.

Published on: