0

The following code works:

@using (Html.BeginForm("MyAction", "MyController", FormMethod.Post, new { enctype = "multipart/form-data", data_ajax = "false" }))
{
    @Html.AntiForgeryToken()
    <input type="file" name="UploadedFile" />
    <input type="submit" value="Upload" class="btn btn-primary" />
}

If I do Request.Files.Count I'll have 1 file in (given that I actually submitted a file in the form, of course), but if I change the data_ajax attribute to true, I never get a file (count is 0), neither through the Request.Files.Count, not through a parameter (where the list would be null) (ie:)

 public ActionResult MyAction(IEnumerable<HttpPostedFileBase> files) {...}

Can anyone explain to me why does one actually submit the right form (with a file), but the other excludes the file from the request?

Toza
  • 1,348
  • 2
  • 14
  • 35
  • file cannot be uplaod with out full post back – Ehsan Sajjad Sep 11 '16 at 16:04
  • @EhsanSajjad Can you explain? – Toza Sep 11 '16 at 17:19
  • 1
    file upload is not possible using Ajax.BeginForm, just google about it, there are plugins and other ways to do file upload via ajax – Ehsan Sajjad Sep 11 '16 at 17:20
  • @EhsanSajjad Ah, alright, didn't know that! Thank you very much for the info! – Toza Sep 11 '16 at 17:30
  • 2
    If you want to upload files using ajax, then you can use `FormData` - refer [this answer](http://stackoverflow.com/questions/29293637/how-to-append-whole-set-of-model-to-formdata-and-obtain-it-in-mvc/29293681#29293681) –  Sep 11 '16 at 21:43

0 Answers0