如何使用AngularJS并调用MVC API下载文件? [英] how to download file using AngularJS and calling MVC API?
本文介绍了如何使用AngularJS并调用MVC API下载文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用AngularJS,我有一个MVC 4 API,它返回的Htt presponseMessage带有附件。
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;
我使用的是jQuery插件称为 fileDownload ......这些精美的下载文件...但我还没有发现在角的方式来做到这一点的方式...任何帮助将AP preciated。
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
推荐答案
我有同样的问题。通过使用所谓的JavaScript库解决了它 FileSaver
I had the same problem. Solved it by using a javascript library called FileSaver
就叫
saveAs(file, 'filename');
展开HTTP POST请求:
$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屋!
查看全文