Is it possible to specify Jquery File Upload to post back only once (for multiple files)?

Go To StackoverFlow.com

0

When I upload multiple files (per blueimp jquery file upload) the [httppost] action is entered once per file. Is it possible to specify one and only one postback with an enumerated file container to iterate?

View:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="@Url.Content("~/Scripts/jquery.ui.widget.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.iframe-transport.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.fileupload.js")" type="text/javascript"></script>

<input id="fileupload" type="file" name="files" multiple="multiple"/>

Controller:

public ActionResult Index()
{
    return View();
}

[HttpPost]
public ActionResult Index(IEnumerable<HttpPostedFileBase> files)
{
    // This is posted back for every file that gets uploaded...I would prefer it only post back once
    // with a collection of files to iterate.  Is this possible?
    foreach (var file in files) // There is only ever one file in files
    {
        var filename = Path.Combine(Server.MapPath("~/App_Data"), file.FileName);
        file.SaveAs(filename);
    }
    return View();
}
2012-04-03 23:25
by JaJ


1

jquery code: create dynamicly fileupload num.

<script type="text/javascript">
$(function() {
    $("#num").append("<option value='0'>select file num...</option>");
    for (var i = 1; i <= 20; i++) {
        $("#num").append("<option value='" + i + "'>" + i + "</option>");
    }
    $("#num").change(function() {
        var num= $("#num").val();
        $("#files").html("");
        for (var i = 0; i < num; i++) {
            $("#files").append("<p><input type='file' name='file_"+i+"'></p>");
        }
    });
});
</script>

// Html codes
<% Html.BeginForm("Upload", "Home", FormMethod.Post, new { @enctype = "multipart/form-data", @id =    "form" }); %>
    <select id="num"></select>
    <div id="files"></div>
    <input type="submit" value="Submit"><%= ViewData["result"] %>
<% Html.EndForm(); %>

controller code:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Upload()
{
  foreach (string name in Request.Files)
  {
    var file = Request.Files[name];
    file.SaveAs(Server.MapPath("~/" + file.FileName));
  }
  ViewData["result"] = "success.";
  return View();
}
2012-04-04 19:24
by AliRıza Adıyahşi