1

I have this kind of problem: In a view I have one button upload which uploads an image and stores it to some folder. Is there a way to call action upload, but not to return a view? Just to save that content, and then I have another button next which will redirect me to a new view. I tried with return type void, but it sends me to blank page.

This are form buttons for upload and next

@using (Html.BeginForm("Upload", "Account", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
    <div class="form-group">
        @Html.LabelFor(model => model.Picture, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            <span class="btn btn-primary btn-file">
                Choose <input type="file" name="file" />
            </span>
            <input type="submit" value="Upload" name="buttonName" class="btn btn-primary"/>
        </div>
     </div>
}


@using (Html.BeginForm("Next", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))

{
    @Html.AntiForgeryToken()
    <div class="form-group">
         <div class="col-md-10">
         <input type="submit" value="Next" name="buttonName" class="btn btn-default" />
         </div>
     </div>
}

This is action:

    public void Upload(HttpPostedFileBase file, string btnSubmit)
    {

        if (file != null && file.ContentLength > 0)
        {
            var fileName = Path.GetFileName(file.FileName);
            var path = Path.Combine(Server.MapPath("~/Images/"), fileName);
            file.SaveAs(path);
        }

    }
liv
  • 11
  • 1
  • Your form method is `Post` which means its making a request to a page, you need to return *something*. What I've done in the past (with async methods) is to return a `ActionResult` and at the very least return a new `HttpStatusCodeResult(HttpStatusCode.OK)` but usually redirect them to the same view with some information about the upload succeeding (or failing). – Ron Beyer Dec 09 '15 at 22:11

4 Answers4

0

You can use the Content() method to return ContentResult ContentResult by default returns a text/plain as its contentType.

public ActionResult Upload(HttpPostedFileBase file, string btnSubmit)
{
    if (file != null && file.ContentLength > 0)
    {
        var fileName = Path.GetFileName(file.FileName);
        var path = Path.Combine(Server.MapPath("~/Images/"), fileName);
        file.SaveAs(path);
    }
    return Content("Success");
}
Orel Eraki
  • 11,940
  • 3
  • 28
  • 36
0

You could return an EmptyResult.

MVC wants you to use EmptyResult when the action is specifically intended to return nothing. Unlike all of the previous ActionResults though, EmptyResult doesn't have a helper. Additionally, if an action returns null, MVC will detect that and make it return an EmptyResult.

momar
  • 364
  • 1
  • 3
0

Ok, I searched about this and I think that i need some ajax without refreshing page. I found this File upload using MVC 4 with Ajax. I'll try it to see if this works.

Community
  • 1
  • 1
liv
  • 11
  • 1
0

The correct solutions are to use either:

return new HttpStatusCodeResult(HttpStatusCode.OK);

to indicate the upload was received and all processing has completed.


return new HttpStatusCodeResult(HttpStatusCode.Accepted);

to indicate the upload was received and further processing will continue.

Chris Marisic
  • 32,487
  • 24
  • 164
  • 258