Asp.net MVC: upload multiple image files?











up vote
17
down vote

favorite
8












is there a good example of how to upload multiple image files in asp.net mvc? I know we can use HttpPostedFileBase to upload one file. Is there a way to upload multiple files by clicking one button?



I used file upload in ajaxtoolbox in webform before and like how it works. Is there a similar way in MVC? or, is there a existing control that can do this well? free control is better, but it is ok even it costs some $.



Thanks










share|improve this question


















  • 1




    we used plupload.com at my last job for multiple uploads
    – Matt Bodily
    Sep 10 '14 at 18:26










  • is there a simple example of how to use it in asp.net MVC? thanks.
    – urlreader
    Sep 10 '14 at 18:47








  • 1




    I don't have access to that code base anymore. Here is an example I found googling pluploadmvc4demo.codeplex.com
    – Matt Bodily
    Sep 10 '14 at 19:42















up vote
17
down vote

favorite
8












is there a good example of how to upload multiple image files in asp.net mvc? I know we can use HttpPostedFileBase to upload one file. Is there a way to upload multiple files by clicking one button?



I used file upload in ajaxtoolbox in webform before and like how it works. Is there a similar way in MVC? or, is there a existing control that can do this well? free control is better, but it is ok even it costs some $.



Thanks










share|improve this question


















  • 1




    we used plupload.com at my last job for multiple uploads
    – Matt Bodily
    Sep 10 '14 at 18:26










  • is there a simple example of how to use it in asp.net MVC? thanks.
    – urlreader
    Sep 10 '14 at 18:47








  • 1




    I don't have access to that code base anymore. Here is an example I found googling pluploadmvc4demo.codeplex.com
    – Matt Bodily
    Sep 10 '14 at 19:42













up vote
17
down vote

favorite
8









up vote
17
down vote

favorite
8






8





is there a good example of how to upload multiple image files in asp.net mvc? I know we can use HttpPostedFileBase to upload one file. Is there a way to upload multiple files by clicking one button?



I used file upload in ajaxtoolbox in webform before and like how it works. Is there a similar way in MVC? or, is there a existing control that can do this well? free control is better, but it is ok even it costs some $.



Thanks










share|improve this question













is there a good example of how to upload multiple image files in asp.net mvc? I know we can use HttpPostedFileBase to upload one file. Is there a way to upload multiple files by clicking one button?



I used file upload in ajaxtoolbox in webform before and like how it works. Is there a similar way in MVC? or, is there a existing control that can do this well? free control is better, but it is ok even it costs some $.



Thanks







asp.net-mvc asp.net-mvc-4 file-upload image-uploading






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Sep 10 '14 at 18:04









urlreader

3,33743257




3,33743257








  • 1




    we used plupload.com at my last job for multiple uploads
    – Matt Bodily
    Sep 10 '14 at 18:26










  • is there a simple example of how to use it in asp.net MVC? thanks.
    – urlreader
    Sep 10 '14 at 18:47








  • 1




    I don't have access to that code base anymore. Here is an example I found googling pluploadmvc4demo.codeplex.com
    – Matt Bodily
    Sep 10 '14 at 19:42














  • 1




    we used plupload.com at my last job for multiple uploads
    – Matt Bodily
    Sep 10 '14 at 18:26










  • is there a simple example of how to use it in asp.net MVC? thanks.
    – urlreader
    Sep 10 '14 at 18:47








  • 1




    I don't have access to that code base anymore. Here is an example I found googling pluploadmvc4demo.codeplex.com
    – Matt Bodily
    Sep 10 '14 at 19:42








1




1




we used plupload.com at my last job for multiple uploads
– Matt Bodily
Sep 10 '14 at 18:26




we used plupload.com at my last job for multiple uploads
– Matt Bodily
Sep 10 '14 at 18:26












is there a simple example of how to use it in asp.net MVC? thanks.
– urlreader
Sep 10 '14 at 18:47






is there a simple example of how to use it in asp.net MVC? thanks.
– urlreader
Sep 10 '14 at 18:47






1




1




I don't have access to that code base anymore. Here is an example I found googling pluploadmvc4demo.codeplex.com
– Matt Bodily
Sep 10 '14 at 19:42




I don't have access to that code base anymore. Here is an example I found googling pluploadmvc4demo.codeplex.com
– Matt Bodily
Sep 10 '14 at 19:42












4 Answers
4






active

oldest

votes

















up vote
4
down vote



accepted










Use this jQuery plugin



just include plugin js files, create tag:



<input type='file' multiple id='fileUpload' name="files" data-url="@Url.Action("Upload","Home")" />


(Except IE9 it is not allowing select multiple files in select dialog)



Add some JavaScript:



$(function () {
$('#fileUpload').fileupload({
dataType: 'json',
done: function (e, data) {
$.each(data.result.files, function (index, file) {
$('<p/>').text(file.name).appendTo(document.body);
});
}
});
});


In controller action just check Request.Files and do whatever you want.
Here is a good documentation



[HttpPost]
public JsonResult Upload()
{
foreach (var file in Request.Files)
{
if(file.ContentLength > 0)
{
file.SaveAs(Server.MapPath("~/Upload/" + file.FileName));
}
}

return Json(new { result = true });
}





share|improve this answer























  • using this plugin, a basic example can be found here : github.com/blueimp/jQuery-File-Upload/wiki/Basic-plugin
    – John Marston
    Mar 3 '17 at 11:42


















up vote
21
down vote













You could implement an action with POST http verb to that receive a collection of HttpPostedFileBase and save all files, for sample:



[HttpPost]
public ActionResult Upload(IEnumerable<HttpPostedFileBase> files)
{
foreach (var file in files)
{
file.SaveAs(Server.MapPath("~/Update/" + file.FileName));
}

return View();
}


Alternatively, you could read Request.Files and do the same job,



[HttpPost]
public ActionResult Upload()
{
foreach (var file in Request.Files)
{
file.SaveAs(Server.MapPath("~/Update/" + file.FileName));
}

return View();
}


See Also




  • Single File Upload to Multiple File Upload in MVC

  • Uploading a File (Or Files) With ASP.NET MVC






share|improve this answer





















  • Thanks a lot for the links, it really helped me
    – magorich
    Dec 7 '16 at 0:03


















up vote
1
down vote













I'm using this one. https://www.fyneworks.com/jquery/multiple-file-upload/



<input type="file" name="file" class="multiple" /> 

[HttpPost]
public ActionResult Upload()
{
if (Request.Files.Count > 0)
{
foreach(var file in Request.Files) { }
}

return View();
}





share|improve this answer




























    up vote
    0
    down vote













    Some of the basic bits required for file uploads



    Notice keyword: multiple in input element AND
    multipart in form element



    HTML Side



    @using (Html.BeginForm("MyUpload", "MyController", FormMethod.Post, new { enctype = "multipart/form-data" })) { 
    <input type="file" name="myFiles" multiple />
    <button class="btn btn-default">Upload</button>
    }




    Controller



    [HttpPost]
    public ActionResult MyUpload(IEnumerable<HttpPostedFileBase> myFiles)
    {
    foreach (var file in myFiles)
    {
    if (file != null && file.ContentLength > 0)
    {
    //handle files;
    }
    }
    return View();
    }





    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














       

      draft saved


      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f25772134%2fasp-net-mvc-upload-multiple-image-files%23new-answer', 'question_page');
      }
      );

      Post as a guest
































      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      4
      down vote



      accepted










      Use this jQuery plugin



      just include plugin js files, create tag:



      <input type='file' multiple id='fileUpload' name="files" data-url="@Url.Action("Upload","Home")" />


      (Except IE9 it is not allowing select multiple files in select dialog)



      Add some JavaScript:



      $(function () {
      $('#fileUpload').fileupload({
      dataType: 'json',
      done: function (e, data) {
      $.each(data.result.files, function (index, file) {
      $('<p/>').text(file.name).appendTo(document.body);
      });
      }
      });
      });


      In controller action just check Request.Files and do whatever you want.
      Here is a good documentation



      [HttpPost]
      public JsonResult Upload()
      {
      foreach (var file in Request.Files)
      {
      if(file.ContentLength > 0)
      {
      file.SaveAs(Server.MapPath("~/Upload/" + file.FileName));
      }
      }

      return Json(new { result = true });
      }





      share|improve this answer























      • using this plugin, a basic example can be found here : github.com/blueimp/jQuery-File-Upload/wiki/Basic-plugin
        – John Marston
        Mar 3 '17 at 11:42















      up vote
      4
      down vote



      accepted










      Use this jQuery plugin



      just include plugin js files, create tag:



      <input type='file' multiple id='fileUpload' name="files" data-url="@Url.Action("Upload","Home")" />


      (Except IE9 it is not allowing select multiple files in select dialog)



      Add some JavaScript:



      $(function () {
      $('#fileUpload').fileupload({
      dataType: 'json',
      done: function (e, data) {
      $.each(data.result.files, function (index, file) {
      $('<p/>').text(file.name).appendTo(document.body);
      });
      }
      });
      });


      In controller action just check Request.Files and do whatever you want.
      Here is a good documentation



      [HttpPost]
      public JsonResult Upload()
      {
      foreach (var file in Request.Files)
      {
      if(file.ContentLength > 0)
      {
      file.SaveAs(Server.MapPath("~/Upload/" + file.FileName));
      }
      }

      return Json(new { result = true });
      }





      share|improve this answer























      • using this plugin, a basic example can be found here : github.com/blueimp/jQuery-File-Upload/wiki/Basic-plugin
        – John Marston
        Mar 3 '17 at 11:42













      up vote
      4
      down vote



      accepted







      up vote
      4
      down vote



      accepted






      Use this jQuery plugin



      just include plugin js files, create tag:



      <input type='file' multiple id='fileUpload' name="files" data-url="@Url.Action("Upload","Home")" />


      (Except IE9 it is not allowing select multiple files in select dialog)



      Add some JavaScript:



      $(function () {
      $('#fileUpload').fileupload({
      dataType: 'json',
      done: function (e, data) {
      $.each(data.result.files, function (index, file) {
      $('<p/>').text(file.name).appendTo(document.body);
      });
      }
      });
      });


      In controller action just check Request.Files and do whatever you want.
      Here is a good documentation



      [HttpPost]
      public JsonResult Upload()
      {
      foreach (var file in Request.Files)
      {
      if(file.ContentLength > 0)
      {
      file.SaveAs(Server.MapPath("~/Upload/" + file.FileName));
      }
      }

      return Json(new { result = true });
      }





      share|improve this answer














      Use this jQuery plugin



      just include plugin js files, create tag:



      <input type='file' multiple id='fileUpload' name="files" data-url="@Url.Action("Upload","Home")" />


      (Except IE9 it is not allowing select multiple files in select dialog)



      Add some JavaScript:



      $(function () {
      $('#fileUpload').fileupload({
      dataType: 'json',
      done: function (e, data) {
      $.each(data.result.files, function (index, file) {
      $('<p/>').text(file.name).appendTo(document.body);
      });
      }
      });
      });


      In controller action just check Request.Files and do whatever you want.
      Here is a good documentation



      [HttpPost]
      public JsonResult Upload()
      {
      foreach (var file in Request.Files)
      {
      if(file.ContentLength > 0)
      {
      file.SaveAs(Server.MapPath("~/Upload/" + file.FileName));
      }
      }

      return Json(new { result = true });
      }






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Sep 20 '15 at 21:59









      Chris Schiffhauer

      12.7k126378




      12.7k126378










      answered Sep 10 '14 at 18:55









      aleha

      4,55911826




      4,55911826












      • using this plugin, a basic example can be found here : github.com/blueimp/jQuery-File-Upload/wiki/Basic-plugin
        – John Marston
        Mar 3 '17 at 11:42


















      • using this plugin, a basic example can be found here : github.com/blueimp/jQuery-File-Upload/wiki/Basic-plugin
        – John Marston
        Mar 3 '17 at 11:42
















      using this plugin, a basic example can be found here : github.com/blueimp/jQuery-File-Upload/wiki/Basic-plugin
      – John Marston
      Mar 3 '17 at 11:42




      using this plugin, a basic example can be found here : github.com/blueimp/jQuery-File-Upload/wiki/Basic-plugin
      – John Marston
      Mar 3 '17 at 11:42












      up vote
      21
      down vote













      You could implement an action with POST http verb to that receive a collection of HttpPostedFileBase and save all files, for sample:



      [HttpPost]
      public ActionResult Upload(IEnumerable<HttpPostedFileBase> files)
      {
      foreach (var file in files)
      {
      file.SaveAs(Server.MapPath("~/Update/" + file.FileName));
      }

      return View();
      }


      Alternatively, you could read Request.Files and do the same job,



      [HttpPost]
      public ActionResult Upload()
      {
      foreach (var file in Request.Files)
      {
      file.SaveAs(Server.MapPath("~/Update/" + file.FileName));
      }

      return View();
      }


      See Also




      • Single File Upload to Multiple File Upload in MVC

      • Uploading a File (Or Files) With ASP.NET MVC






      share|improve this answer





















      • Thanks a lot for the links, it really helped me
        – magorich
        Dec 7 '16 at 0:03















      up vote
      21
      down vote













      You could implement an action with POST http verb to that receive a collection of HttpPostedFileBase and save all files, for sample:



      [HttpPost]
      public ActionResult Upload(IEnumerable<HttpPostedFileBase> files)
      {
      foreach (var file in files)
      {
      file.SaveAs(Server.MapPath("~/Update/" + file.FileName));
      }

      return View();
      }


      Alternatively, you could read Request.Files and do the same job,



      [HttpPost]
      public ActionResult Upload()
      {
      foreach (var file in Request.Files)
      {
      file.SaveAs(Server.MapPath("~/Update/" + file.FileName));
      }

      return View();
      }


      See Also




      • Single File Upload to Multiple File Upload in MVC

      • Uploading a File (Or Files) With ASP.NET MVC






      share|improve this answer





















      • Thanks a lot for the links, it really helped me
        – magorich
        Dec 7 '16 at 0:03













      up vote
      21
      down vote










      up vote
      21
      down vote









      You could implement an action with POST http verb to that receive a collection of HttpPostedFileBase and save all files, for sample:



      [HttpPost]
      public ActionResult Upload(IEnumerable<HttpPostedFileBase> files)
      {
      foreach (var file in files)
      {
      file.SaveAs(Server.MapPath("~/Update/" + file.FileName));
      }

      return View();
      }


      Alternatively, you could read Request.Files and do the same job,



      [HttpPost]
      public ActionResult Upload()
      {
      foreach (var file in Request.Files)
      {
      file.SaveAs(Server.MapPath("~/Update/" + file.FileName));
      }

      return View();
      }


      See Also




      • Single File Upload to Multiple File Upload in MVC

      • Uploading a File (Or Files) With ASP.NET MVC






      share|improve this answer












      You could implement an action with POST http verb to that receive a collection of HttpPostedFileBase and save all files, for sample:



      [HttpPost]
      public ActionResult Upload(IEnumerable<HttpPostedFileBase> files)
      {
      foreach (var file in files)
      {
      file.SaveAs(Server.MapPath("~/Update/" + file.FileName));
      }

      return View();
      }


      Alternatively, you could read Request.Files and do the same job,



      [HttpPost]
      public ActionResult Upload()
      {
      foreach (var file in Request.Files)
      {
      file.SaveAs(Server.MapPath("~/Update/" + file.FileName));
      }

      return View();
      }


      See Also




      • Single File Upload to Multiple File Upload in MVC

      • Uploading a File (Or Files) With ASP.NET MVC







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Sep 10 '14 at 20:31









      Felipe Oriani

      27.7k1595155




      27.7k1595155












      • Thanks a lot for the links, it really helped me
        – magorich
        Dec 7 '16 at 0:03


















      • Thanks a lot for the links, it really helped me
        – magorich
        Dec 7 '16 at 0:03
















      Thanks a lot for the links, it really helped me
      – magorich
      Dec 7 '16 at 0:03




      Thanks a lot for the links, it really helped me
      – magorich
      Dec 7 '16 at 0:03










      up vote
      1
      down vote













      I'm using this one. https://www.fyneworks.com/jquery/multiple-file-upload/



      <input type="file" name="file" class="multiple" /> 

      [HttpPost]
      public ActionResult Upload()
      {
      if (Request.Files.Count > 0)
      {
      foreach(var file in Request.Files) { }
      }

      return View();
      }





      share|improve this answer

























        up vote
        1
        down vote













        I'm using this one. https://www.fyneworks.com/jquery/multiple-file-upload/



        <input type="file" name="file" class="multiple" /> 

        [HttpPost]
        public ActionResult Upload()
        {
        if (Request.Files.Count > 0)
        {
        foreach(var file in Request.Files) { }
        }

        return View();
        }





        share|improve this answer























          up vote
          1
          down vote










          up vote
          1
          down vote









          I'm using this one. https://www.fyneworks.com/jquery/multiple-file-upload/



          <input type="file" name="file" class="multiple" /> 

          [HttpPost]
          public ActionResult Upload()
          {
          if (Request.Files.Count > 0)
          {
          foreach(var file in Request.Files) { }
          }

          return View();
          }





          share|improve this answer












          I'm using this one. https://www.fyneworks.com/jquery/multiple-file-upload/



          <input type="file" name="file" class="multiple" /> 

          [HttpPost]
          public ActionResult Upload()
          {
          if (Request.Files.Count > 0)
          {
          foreach(var file in Request.Files) { }
          }

          return View();
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Sep 10 '14 at 22:17









          Kadir

          1,80912642




          1,80912642






















              up vote
              0
              down vote













              Some of the basic bits required for file uploads



              Notice keyword: multiple in input element AND
              multipart in form element



              HTML Side



              @using (Html.BeginForm("MyUpload", "MyController", FormMethod.Post, new { enctype = "multipart/form-data" })) { 
              <input type="file" name="myFiles" multiple />
              <button class="btn btn-default">Upload</button>
              }




              Controller



              [HttpPost]
              public ActionResult MyUpload(IEnumerable<HttpPostedFileBase> myFiles)
              {
              foreach (var file in myFiles)
              {
              if (file != null && file.ContentLength > 0)
              {
              //handle files;
              }
              }
              return View();
              }





              share|improve this answer

























                up vote
                0
                down vote













                Some of the basic bits required for file uploads



                Notice keyword: multiple in input element AND
                multipart in form element



                HTML Side



                @using (Html.BeginForm("MyUpload", "MyController", FormMethod.Post, new { enctype = "multipart/form-data" })) { 
                <input type="file" name="myFiles" multiple />
                <button class="btn btn-default">Upload</button>
                }




                Controller



                [HttpPost]
                public ActionResult MyUpload(IEnumerable<HttpPostedFileBase> myFiles)
                {
                foreach (var file in myFiles)
                {
                if (file != null && file.ContentLength > 0)
                {
                //handle files;
                }
                }
                return View();
                }





                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  Some of the basic bits required for file uploads



                  Notice keyword: multiple in input element AND
                  multipart in form element



                  HTML Side



                  @using (Html.BeginForm("MyUpload", "MyController", FormMethod.Post, new { enctype = "multipart/form-data" })) { 
                  <input type="file" name="myFiles" multiple />
                  <button class="btn btn-default">Upload</button>
                  }




                  Controller



                  [HttpPost]
                  public ActionResult MyUpload(IEnumerable<HttpPostedFileBase> myFiles)
                  {
                  foreach (var file in myFiles)
                  {
                  if (file != null && file.ContentLength > 0)
                  {
                  //handle files;
                  }
                  }
                  return View();
                  }





                  share|improve this answer












                  Some of the basic bits required for file uploads



                  Notice keyword: multiple in input element AND
                  multipart in form element



                  HTML Side



                  @using (Html.BeginForm("MyUpload", "MyController", FormMethod.Post, new { enctype = "multipart/form-data" })) { 
                  <input type="file" name="myFiles" multiple />
                  <button class="btn btn-default">Upload</button>
                  }




                  Controller



                  [HttpPost]
                  public ActionResult MyUpload(IEnumerable<HttpPostedFileBase> myFiles)
                  {
                  foreach (var file in myFiles)
                  {
                  if (file != null && file.ContentLength > 0)
                  {
                  //handle files;
                  }
                  }
                  return View();
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jul 3 '17 at 13:15









                  Moji

                  3,14322628




                  3,14322628






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f25772134%2fasp-net-mvc-upload-multiple-image-files%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest




















































































                      Popular posts from this blog

                      Florida Star v. B. J. F.

                      Danny Elfman

                      Lugert, Oklahoma