javascript - Post files and object in the same AJAX request -
i have data , files should posted in 1 same ajax
request. tried solutions null
in controller (files sent fine , when change tester
class simple string
works).
my js:
function sendajaxwithfile (formdata, successcallback, errorcallback) { successcallback = successcallback || function () { }; errorcallback = errorcallback || function () { }; $.ajax({ url: "/test/save", type: "post", data: formdata, cache: false, processdata: false, contenttype: false, success: successcallback, error: errorcallback }); } var data = new formdata(); data.append("tester", { name: "qwerty" }); data.append("somefile", file); $scope.sendajaxwithfile(data, function (response, textstatus, jqxhr) { }, function (jqxhr, textstatus, errorthrown) { });
and server side code:
[ajaxonly] [httppost] [route("save")] public async task<jsonresult> save(tester tester, httppostedfilebase somefile) { return await task.fromresult(json(0)); }
tester
class:
public class tester { public string name { get; set; } public datetime date { get; set; } }
i did workaround, because don't think can resolved in normal way...
my solution send string data serialized js
object.
data.append("serializedjsonstring", json.stringify({ name: "qwerty" })); // can serialize want
i needed change parameter of save
method json
string data , convert tester
class manually:
[ajaxonly] [httppost] [route("save")] public async task<jsonresult> save(string serializedjsonstring, httppostedfilebase somefile) { var decodedjson = httputility.urldecode(serializedjsonstring); var jtester = jobject.parse(decodedjson); var tester = new tester { name = jtester["name"].tostring() }; return await task.fromresult(json(0)); }
Comments
Post a Comment