Skip to main content

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.

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
  26. Hello,
    this is working but it is Perfect when we create web application but not working in website..any solution for these...

    ReplyDelete
  27. It should work on anything. Can you please double check your code.

    ReplyDelete
  28. its working... I want to show percantages with uploading any solution for this

    ReplyDelete
  29. I want to show percantages with uploading any solution for this

    ReplyDelete
  30. Hello , I want to progress bar with this control. can i use it.. please give any solution.

    ReplyDelete
  31. Hello ,
    Its Working..

    I want to progress bar with this control. can i use it.. please give any solution.
    Thanks

    ReplyDelete

Post a Comment

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

Popular posts from this blog

How to create Rest API(Web API) with ASP.NET Core 1.0

Recently Microsoft has released ASP.NET Core 1.0 RC2, I am getting lots of request from readers that about creating Web API. So I thought it will be a good idea to write a blog post about how to create Rest API(Web API) with ASP.NET Core 1.0.

So let’s get started via creating an ASP.NET Core 1.0 Web Application like following.



Once you click on ASP.NET Web Application, It will ask whether you need to create Web Application or Web API application. We are going to Web API so I am going to select Web API Application like following. Please note that in ASP.NET Core 1.0 there is no separate libraries or DLLs required for creating web APIs. This is just a project template.



Now once you click on OK It will create a Web API application with default values controller and program.cs.  As you know Program.cs is now starting point for the ASP.NET Core 1.0 application so It contains all the required configuration and startup items. Following is a code for that.
using System.IO; using Microsoft.Asp…

How to use migration with Entity Framework Core

Entity framework core is  the lightweight, extensible and cross-platform version of Entity Framework. Before some time, Microsoft has Released a new version of Entity Framework RC2. I have written a couple of blog post about Entity framework code first migration earlier for Entity framework 6.0. So there was a couple of request coming for writing a blog post about Entity Framework Core RC2 migration. So I thought it will be a good idea to give an overview how database migration works in Entity Framework Core RC2. This post will cover a basic scenario where we are going to create the database with existing ASP.NET Identity migration and then we are going to create a new model and have that migration applied in the database.

How to use Entity Framework Migrations: Let’s get started, To demonstrate entity framework core migrations, I am going to create a sample asp.net core web application like following.



Once we select asp.net core application it will appear the following dialog.



Now w…

Creating PDF with ASP.Net MVC and RazorPDF

Update: I have written a new blog post about better approach to create a PDF with asp.net mvc- You can find that following location.- A Better Solution to create PDF with Rotativa and ASP.NET MVC
In this post we are going to learn how we can easily create PDF from ASP.Net application with the help of Razor PDF NuGet package.

About Razor PDF:
This NuGet package is created by Al Nyveldt It internally uses ITextSharp an open source PDF convertor library. RazorPDF uses Razor View engine to create iTextXML which is in tern used to produce PDF file. You can get more information about that at below link.

https://www.nuget.org/packages/RazorPDF

Example(Creating PDF with ASP.Net MVC):
So what we are we waiting for ? Let’s create a simple example. To create example first thing we need to a create and ASP.Net MVC application.