Asp.net MVC: upload multiple image files?












17














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
















17














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














17












17








17


9





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,37443458




3,37443458








  • 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


















5














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



















23














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



















1














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





























    0














    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',
      autoActivateHeartbeat: false,
      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















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      5














      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
















      5














      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














      5












      5








      5






      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.9k126378




      12.9k126378










      answered Sep 10 '14 at 18:55









      aleha

      4,68211828




      4,68211828












      • 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













      23














      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
















      23














      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














      23












      23








      23






      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.9k1795155




      27.9k1795155












      • 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











      1














      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


























        1














        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
























          1












          1








          1






          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,83912643




          1,83912643























              0














              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


























                0














                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
























                  0












                  0








                  0






                  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,18822628




                  3,18822628






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      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















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Schultheiß

                      Liste der Kulturdenkmale in Wilsdruff

                      Android Play Services Check