User Id :    Password :      New Member   Forgot Password  
 
Multiple file upload with asp.net 4.5 and Visual Studio 2012
Description In earlier version of ASP.NET there is no way to upload multiple files at same time. We need to use third party control or we need to create custom control for that. But with asp.net 4.5 now its possible to upload multiple file with file upload control.   No. of Views     2601
  Rating     0
Author Jalpesh Vadgama   Posted On     12 Jul 2012
Tags ASP.NET,ASP.NET : How To    

Sample Code   Download Code

With ASP.NET 4.5 version Microsoft has enhanced file upload control to support HTML5 multiple attribute. There is a property called ‘AllowedMultiple’ to support that attribute and with that you can easily upload the file. So what we are waiting for!! It’s time to create one example.
On the default.aspx file I have written following.

 

<asp:FileUpload ID="multipleFile" runat="server" AllowMultiple="true" />
<asp:Button ID="uploadFile" runat="server" Text="Upload files" onclick="uploadFile_Click"/>

 

Here you can see that I have given file upload control id as multipleFile and I have set AllowMultiple file to true. I have also taken one button for uploading file.For this example I am going to upload file in images folder. As you can see I have also attached event handler for button’s click event. So it’s time to write server side code for this. Following code is for the server side.

 

protected void uploadFile_Click(object sender, EventArgs e)
{
    if (multipleFile.HasFiles)
    {
        foreach(HttpPostedFile uploadedFile in multipleFile.PostedFiles)
        {
            uploadedFile.SaveAs(System.IO.Path.Combine(Server.MapPath("~/Images/"),uploadedFile.FileName));
            Response.Write("File uploaded successfully");
        }
    }
}

 

Here in the above code you can see that I have checked whether multiple file upload control has multiple files or not and then I have save that in Images folder of web application. Once you run the application in browser it will look like following.

 

 

I have selected two files. Once I have selected and clicked on upload file button it will give message like following.

 

 

As you can see now it has successfully upload file and you can see in windows explorer like following.

 

 

As you can see it’s very easy to upload multiple file in ASP.NET 4.5. Stay tuned for more. Till then happy programming.

 

P.S.: This feature is only supported in browser who support HTML5 multiple file upload. For other browsers it will work like normal file upload control in asp.net.

About Author

About Author I am jalpesh vadgama an Microsoft MVP for Visual C# and BrainBench Certified ASP.NET Developer having more then 5 years of experience in .NET Technology.Please feel free to contact me for any queries via posting comments on my blog I will try to reply as early as possible. Please also visit my blog at http://jalpesh.blogspot.com/ Jalpesh Vadgama
No Photo
 
Country India
Company N/A
Home Page http://jalpesh.blogspot.com/

Rate this article

Rating options from poor, fair, good, very good to excelent.  
 

Comments

   
Write your comment here.
Comment
Verification Code