Multiple file upload with asp.net 4.5 and Visual Studio 2012

Note: This post will be part of Visual Studio 2012 feature series.
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.

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.

Multiple file upload with asp.net 4.5 and Visual Studio 2012

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

Multiple file upload with ASP.NET 4.5 and HTML5

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

Multiple file upload ASP.NET 4.5 and Visual Studio 2012

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.

38 comments:

  1. Do an MVC version please.

    ReplyDelete
  2. Ciwan Kurd - In next post I will do MVC one.

    ReplyDelete
  3. I built something similar for other platforms becuase of the limited .NET upload. Nice to see this. Will their be any chance to see a thumbnail preview of the image before it is uploaded?

    ReplyDelete
  4. I built something similar for other platforms becuase of the limited .NET upload. Nice to see this. Will their be any chance to see a thumbnail preview of the image before it is uploaded?

    ReplyDelete
  5. I recommand to update the example and save the uploaded files to a sub folder of App_Data folder

    ReplyDelete
  6. Good tip. Your blog is too slow.

    ReplyDelete
  7. that's great Addition in the New Version of VS.

    ReplyDelete
  8. This isn't .NET 4.5 only, it will work on any version,

    ReplyDelete
  9. Any progress bar included for each file? AJAX toolkit version of file upload control has progress bar for every file.

    ReplyDelete
  10. great, in the next version they should add a progress bar to the upload control

    ReplyDelete
  11. Shafaqat Ali - By default its not there like this is a basic progress bar.

    ReplyDelete
  12. Dave - Other version does not allow multiple attribute. But off course you add attribute from server side.

    ReplyDelete
  13. Basslk - this is a just simple change. You just need to modify the path.

    ReplyDelete
  14. Yes you can have that in session and then once you click save.

    ReplyDelete
  15. http://www.dotnetjalps.com/2012/07/Multiple-file-upload-with-asp-net-mvc-and-HTML5.html - this a asp.net mvc version of multiple file upload.

    ReplyDelete
  16. Please guys , i need help on uploading profile picture on my MVC application, i used an extension helper method and called it in the view,but the pix didn't display.Please i need ur help guys. i use VB

    ReplyDelete
  17. @3bb5345f335a11bba6062a96d0cb9357:disqus - There is already a blog post written with same example with asp.net mvc. Following is a URL for that-http://www.dotnetjalps.com/2012/07/Multiple-file-upload-with-asp-net-mvc-and-HTML5.html

    ReplyDelete
  18. very informative post indeed.. being enrolled in http://www.wiziq.com/course/57-fresher-training-projects was looking for such articles online to assist me.. and your post helped me a lot

    ReplyDelete
  19. One of my reader ask me that does it support IE8- My answer is no. Please read following question-
    http://stackoverflow.com/questions/5987936/html5-input-type-files-multiple-attribute-not-working-in-ie

    ReplyDelete
  20. I opened Framework 4.0 project in 4.5 to use the multiple fileupload feature and it says 'System.Web.UI.WebControls.FileUpload' does not contain a definition for 'HasFiles' and System.Web.UI.WebControls.FileUpload' does not contain a definition for 'PostedFiles'. Is there a solution to get rid of this?

    ReplyDelete
  21. Very good publish. I just came upon your website and desired to say that I have really experienced studying your websites. Any way I’ll be signing up for your supply and I wish you publish again soon.

    ReplyDelete
  22. 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.

    ReplyDelete
  23. Thats true, but for shared hosting and some providers like the one I'm using you cant upload directly to your any folder through your code

    ReplyDelete
  24. i am trying to upload multiple file at a time with the using this example but i am having problem with Foreach.
    it is showing error foreach statement can not contain on variable of type System.web.httpfile bacuse system.web.httpfile dose not contain a public definition for Get Enumerator

    ReplyDelete
  25. Great share.. I really enjoy your article and your blog.. I can updated with all new technology with your blog... can i share this article on my blog www.dotnetspan.com

    ReplyDelete
    Replies
    1. There is no problem to share with proper credits!!. You need to put my blog link there and then you can share!!

      Delete

Your feedback is very important to me. Please provide your feedback via putting comments.