Question

I'm using jquery file uploader (per blueimp) and I'm wondering how I can direct the postback to a specificed http post back other than index.cshtml (post is going to index.cshtml for by default)?

For instance when calling Home/Bulk...then after selecting files the postback is defaulting to [httppost]index.cshtml()...How can I direct it to [httppost]bulk.cshtml()? Thx!

View (Bulk.cshtml):

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


@using (Html.BeginForm("Bulk", "Home"))  // I want this to direct back to httppost:bulk handler
{
<input id="fileupload" type="file" name="files" multiple="multiple"/>
}

Controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Index(IEnumerable<HttpPostedFileBase> files)
    {
        foreach (var file in files)
        {
            var filename = Path.Combine(Server.MapPath("~/App_Data"), file.FileName);
            file.SaveAs(filename);
        }
        return View();
    }

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

    [HttpPost]
    public ActionResult Bulk(IEnumerable<HttpPostedFileBase> files)
    {
        foreach (var file in files)
        {
            var filename = Path.Combine(Server.MapPath("~/App_Data"), file.FileName);
            file.SaveAs(filename);
        }
        return View();
    }

}
Was it helpful?

Solution

You could use the url property when setting up the plugin to indicate to which action you want it to submit:

$('#fileupload').fileupload({
    url: 'home/bulk',
    done: function (e, data) {
    }
});

OTHER TIPS

Did you mean you wanted to redirect after the post? Perhaps something like this using the Post-Redirect-Get pattern. Note the different return.

[HttpPost]
public ActionResult Bulk(IEnumerable<HttpPostedFileBase> files)
{
    foreach (var file in files)
    {
        var filename = Path.Combine(Server.MapPath("~/App_Data"), file.FileName);
        file.SaveAs(filename);
    }
    return RedirectToAction("Bulk");
}

EDIT

Ah, I see. This is most likely because your html form isn't setup correctly. As Darin suggests, you may use jQuery. But, if you wish to use the html helper you can do this:

@using (Html.BeginForm("Bulk", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))

The multipart/form-data is important as it facilitates allowing the files to be sent to the controller.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top