如何使用 AngularJS 下载文件并调用 MVC API? [英] how to download file using AngularJS and calling MVC API?
本文介绍了如何使用 AngularJS 下载文件并调用 MVC API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用的是 AngularJS,我有一个 MVC 4 API,它返回一个带有附件的 HttpResponseMessage.
I am using AngularJS, and I have a MVC 4 API that returns a HttpResponseMessage with an attachment.
var result = new MemoryStream(pdfStream, 0, pdfStream.Length) {
Position = 0
};
var response = new HttpResponseMessage {
StatusCode = HttpStatusCode.OK,
Content = new StreamContent(result)
};
response.Content.Headers.ContentDisposition =
new ContentDispositionHeaderValue("attachment") {
FileName = "MyPdf.pdf"
};
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
return response;
我正在使用一个名为 fileDownload 的 jQuery 插件......它可以很好地下载文件......但我还没有找到以角度"方式执行此操作的方法......任何帮助将不胜感激.
I am using a jQuery plugin called fileDownload... which download the file beautifully... but I havent found the way to do this in the "Angular" way... any help will be appreciated.
//_e
推荐答案
我遇到了同样的问题.使用名为 FileSaver
I had the same problem. Solved it by using a javascript library called FileSaver
直接打电话
saveAs(file, 'filename');
完整的 http 发布请求:
$http.post('apiUrl', myObject, { responseType: 'arraybuffer' })
.success(function(data) {
var file = new Blob([data], { type: 'application/pdf' });
saveAs(file, 'filename.pdf');
});
这篇关于如何使用 AngularJS 下载文件并调用 MVC API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文