We are trying to download a file from a web api v2 and then either use a 'Save As' OR automatically download in browser any solution would be fine but cannot get any to work.
What is happening in Chrome is nothing, doesn't do a thing In IE we get a prompt 'do you want to allow this website to open an application on your computer?' click allow and then nothing happens at all.
Here is the WebAPI controller code:
[HttpGet]
[Route("{pdf}/{getPDF}")]
public async Task<IHttpActionResult> GetPDF()
{
var outputpath = HttpContext.Current.Server.MapPath("~/");
HttpResponseMessage result;
using (FileStream stream = new FileStream(outputpath + "Inv_008618_180868_33687.pdf", FileMode.Open))
{
result = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StreamContent(stream),
};
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "Inv_008618_180868_33687.pdf",
Size = stream.Length
};
return Ok(result);
}
}
I think the above code is correct not 100% sure though as the filesize is very small.
Here is the Angular code:
getPDF: function () {
var scope = this;
var urlApi = 'http://localhost:59242/supplier/pdf/';
return $http({
url: urlApi,
responseType: 'arraybuffer'
}).then(function (response) {
var file = new Blob([response], { type: 'application/pdf' });
var fileURL = URL.createObjectURL(file);
var pdf = $sce.trustAsResourceUrl(fileURL);
//var element = angular.element('<a/>');
//element.attr({
// href: 'data:attachment/pdf;charset=utf-8,' + encodeURI(response),
// target: '_self',
// download: 'test.pdf'
//})[0].click();
window.open(pdf);
}
return response.data;
})
.catch(function (err) {
})
}
I would like to thank you for replying
使用 ngFileSaver 解决了这个问题,谢谢
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.