Uploading Multiple Files in ASP.NET 2.0

In ASP.NET 2.0, the FileUpload control enables users to upload file from your web pages. The FileUpload control consists of a text box and a browse button. Clicking on the button allow users to select a file on the client and upload it to the server.

Let us start how to upload multiple file on a single button click.  Follow these 2 steps

Step 1: Drag and drop multiple (in my case four) FileUpload controls on to the designer.
Step 2: Drop a Button control and rename it to "Upload"

This is the aspx code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>To Upload Multiple File in ASP.NET 2.0</title>

</head>

<body>

    <form id="form1" runat="server">

        <div>

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

            <asp:FileUpload ID="FileUpload2" runat="server" /><br />

            <asp:FileUpload ID="FileUpload3" runat="server" /><br />

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

            <br />

            <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click1" />

            <br />

            <asp:Label ID="Label1" runat="server"></asp:Label>

        </div>

    </form>

</body>

</html>


This is the aspx.cs code

 

Firstly add a name space

using System.IO;

 

Now the code is

 

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.IO;

 

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

    }

   

    protected void btnUpload_Click1(object sender, EventArgs e)

    {

        try

        {    

            //To Upload Multiple Files on Single Click

            HttpFileCollection hfc = Request.Files;

            for (int i = 0; i < hfc.Count; i++)

            {

                HttpPostedFile hpf = hfc[i];

                if (hpf.ContentLength > 0)

                {

                    hpf.SaveAs(Server.MapPath("MyFiles") + "\\" + Path.GetFileName(hpf.FileName));

                }

            }

        }

        catch (Exception ex)

        {          

        }

 

    }

}


Add a tag in web.config file 

<httpRuntime

               executionTimeout="200"

               maxRequestLength="8192"

               requestLengthDiskThreshold="256"

               useFullyQualifiedRedirectUrl="false"

               minFreeThreads="8"

               minLocalRequestFreeThreads="4"

               appRequestQueueLimit="5000"

               enableKernelOutputCache="true"

               enableVersionHeader="true"

               requireRootedSaveAsPath="true"

               enable="true"

               shutdownTimeout="90"

               delayNotificationTimeout="5"

               waitChangeNotification="0"

               maxWaitChangeNotification="0"

               enableHeaderChecking="true"

               sendCacheControlHeader="true"

               apartmentThreading="false"/>


 

When you run the application then

UploadMultipleFile.JPG 

Continue to next part >>  

Up Next
    Ebook Download
    View all
    Learn
    View all