Mvc4 webapi file download with jquery
- by Ray
I want to download file on client side from api
apicontroller:
public HttpResponseMessage PostOfficeSupplies()
{
string csv = string.Format ("D:\\Others\\Images/file.png");
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new StringContent(csv);
result.Content.Headers.ContentType = new MediaTypeHeaderValue ("application/octet-stream");
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentDisposition.FileName = "file.png";
return result;
}
1.How can I popup a download with jquery(octet-stream) from api controller?
my client side code:
$(document).ready(function () {
$.ajax(
{
url: 'api/MyAPI'
, type: "post"
, contentType: "application/octet-stream"
, data: ''
, success:
function (retData) {
$("body").append("<iframe src='" + retData + "' style='display: none;' ></iframe>");
}
});
});
but it was not work!!!Thanks!!