0

Edit: I would like this answer in jquery not a plain javascript ajax request.

I have looked at stackoverflow examples but none of them are working. Not sure what I am doing wrong.

I am posting a file back to an asp.net mvc controller.

Jquery

   ...
                var uploadFile = document.getElementById('js-upload-files').files[0];

                var $formData = new FormData();
                $formData.append('file', uploadFile);
                component.startUpload($formData);
    ...
            startUpload : function (formData) {
                DataConnectAjax.ajaxRequest($fileUploadUrl,
                    formData,
                    null,
                    "File Upload",
                    "Success",
                    null);
            }

DataConnectAjax is just a normal jquery.ajax with a toast message at the end.

Controller

      public ActionResult FileUpload()
        {
            var file = Request.Files["file"]; //file[0] same result null
...
            return new BetterJsonResult();
        }

file variable is null. I tried passing it in the constructor using HttpFileBase Or something but still the same result.

Train
  • 3,420
  • 2
  • 29
  • 59
  • Possible duplicate of [jQuery ajax upload file in asp.net mvc](http://stackoverflow.com/questions/2428296/jquery-ajax-upload-file-in-asp-net-mvc) – ADreNaLiNe-DJ Jun 08 '16 at 14:43
  • WHat is `DataConnectAjax` and what ajax options is it setting. Refer [this answer](http://stackoverflow.com/questions/29293637/how-to-append-whole-set-of-model-to-formdata-and-obtain-it-in-mvc/29293681#29293681) for the necessary ajax options to upload a file. –  Jun 08 '16 at 23:46
  • ajax paramaters are `url, model, divToUpdateIfReturningAPartialView, "toast message on complete", "another toast message oncomplete", callback method` – Train Jun 09 '16 at 00:29
  • 1
    You need to ensure that the following ajax options are set - `processData: false,` and `contentType: false,` –  Jun 09 '16 at 01:28
  • @Stephen Muecke Thank you, that worked.The Ajax options `processData` was not set and my contetType was `application/json`. – Train Jun 09 '16 at 03:42

0 Answers0