asp.net mvc - AJAX POST does not return a file download -
i have post ajax request in asp.net mvc application, works this:
$.ajax({ type: "post", url: 'https://localhost:44300/api/values', data: ruledata, contenttype: "application/json; charset=utf-8" });
in case, ruledata
encompasses json blob:
var ruledata = json.stringify({ 'targetproduct': "someproduct", 'content': editor.getvalue(), 'targetproductversion' : "1.0" });
the request backed post handled in web api application, returns pushstreamcontent
appropriate headers.
return new httpresponsemessage(httpstatuscode.ok) { content = pushstreamcontent};
in fiddler, see response web api coming application/octet-stream
, in right size expecting (it generates zip file in backend), there never dialog shown allow user download file.
what missing?
solved xhr
var xhr = new xmlhttprequest(); xhr.responsetype = "blob"; xhr.onreadystatechange = function(){ if (this.readystate == 4) { if (this.status == 200) { download(xhr.response, "bundle.zip", "application/zip"); } else if (this.status == 400) { var reader = new filereader(); reader.onload = function(e) { var text = reader.result; alert(text); } reader.readastext(xhr.response); } } } xhr.open('post', 'https://localhost:44300/api/values'); xhr.setrequestheader('content-type', 'application/json; charset=utf-8'); xhr.responsetype = 'blob'; // response blob , not text xhr.send(ruledata);
Comments
Post a Comment