ajax - File posted to MVC Controller is null -


i want post 3 things mvc controller: 1 image , 2 strings.

on view, i've got form uses enctype="multipart/form-data" automatically submits form after image file selected. submit handler form:

    $("#photouploadform").on("submit", function (event) {         event.preventdefault();          var imagedata = $("#photouploadfileinput").val();         var guestnumber = $("#guestid").val();         var tcsa_id = vm.getselectedtreatmentareatcsa_id(vm.photographs.selectedtreatmentarea());          var dto = {             imagedata: imagedata,             guestnumber: guestnumber,             tcsa_id: tcsa_id         }          $.ajax({             url: 'saveimage',             type: "post",             contenttype: "multipart/form-data",             data: ko.tojson(dto),             success: function (data) {                 console.log(submitted);             }         });     }); 

the dto object defined in model:

public class photouploaddto {     public httppostedfilebase imagedata { get; set; }     public string guestnumber { get; set; }     public string tcsa_id { get; set; } } 

and in controller, have action takes in dto parameter:

   public actionresult saveimage(photouploaddto dto)    {      //etc.    } 

when try post dto, gets posted null. problematic me because want able post image , 2 strings controller simultaneously.

i suspect issue var imagedata (which set value of <input type="file" id="photouploadfileinput"> on view), , being posted c:/fakepath/etc. not actual image file. it's frustrating because know wouldn't issue if had form just posted image, need use submit handler , don't know how bring actual image data it.

why data null when hits mvc controller, , how can post these 3 items while still being able use submit handler?

uploading file via ajax tricky thing. of modern web browsers handle using file api indeed work uploading file via ajax. however, using solution not work people on older browsers.

your best bet using jquery plugin or similar fall on techniques such uploading file via iframe or other workarounds.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -