How to upload multiple files to server with form post MVC?

I am working on a project of Asp.Net MVC-4. In my project user post requirements. Post contains title, tags, files etc.File may be multiple and it may be of any type like Video, doc ( ppt, excel, pdf, etc), images etc.

My problem is the handling of multiple file upload. Now first of all i tell you

currently how i am handling this :

I am using Jquery FIle Uplaod plugin. Through this plugin i am uploading file to server sequentially and on server i am saving those file with SessionId.Now when user post there requirement form than i just rename those file with userId.

drawback of my approach

First i have to save those files with session id and than i have to rename it with userId. So if i save my file in Window Azure Blobs than in that case for uploading single file i have to do 2 transaction. First save the blob with SessionId and than Renaming the blob with userid. Which i think result extra processing and extra cost.

Now i want to know if there is any approach by which i can upload all file (with progress bar for individual file [required]) with form post. So that user's requirement form (tags, titile etc) with all files go to server together than in that case i will save the user first in the database and than i will save the files with the userId ??

Note: I cannot save File with guid or other. UserId is required in the file name to uniquely identify user's files.


Not sure if i completely understand the question, but you could post the model for your user details in the same POST as the file upload (same form?), then on the server:

    public JsonResult AddNewImage(UserModel user)
        ReturnArgs r = new ReturnArgs();
        repo.AddUser(user) // add your user to DB here

        r.Status = 200;
        r.Message = "OK!";

        return Json(r);

    private void SaveImages(string userid)
        for (var i = 0; i < Request.Files.Count; i++)
            var file = Request.Files[i] as HttpPostedFileBase;
            string fileName = userid + "_" + i;

            // saving file to DB here, but you can do what you want with
            // the inputstream

            repo.SaveImage(fileName, file.InputStream, file.ContentType);

You can upload using following code in controller

public ActionResult Index(IEnumerable<HttpPostedFileBase> files) {
  foreach (var file in files) {
    if (file.ContentLength > 0) {
      var fileName = Path.GetFileName(file.FileName);
      var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
  return RedirectToAction("Index");

more details here

Need Your Help

Why does my multi-threaded app sometimes hang when it closes?

multithreading delphi critical-section

I'm using several critical sections in my application. The critical sections prevent large data blobs from being modified and accessed simultaneously by different threads.

jQuery 1.3.2 and IE 8 problem with hide() and show()

jquery internet-explorer-8 hover hide show

Can someone tell me please why the following example work in Firefox but not in IE 8?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.