我正在尝试从asp.net控制器ActionResult下载一个文件,但是如何从浏览器下载呢?
我正在返回控制器级别的文件
return File(pdfStream, "application/pdf", "sample.pdf");关于角码
reportService.getReport().then(function(file){
//how to make this file download
})我试过使用此代码,但只在浏览器为chrome但文件内容为空时才下载pdf。
reportService.getReport().then(function (file) {
var hiddenElement = document.createElement('a');
hiddenElement.href = 'data:attachment/pdf,' + encodeURI(file);
hiddenElement.target = '_blank';
hiddenElement.download = 'myFile.pdf';
hiddenElement.click();
});响应头看起来像

任何帮助都感谢!!
谢谢
发布于 2014-11-26 12:23:44
假设您使用的是$http.post
您可以使用FileSaver.js https://github.com/eligrey/FileSaver.js
并将响应类型设置为blob。
就像这样,
reportService.getReport().then(function(url, data){
return $http.post(url, data, { responseType:'blob' })
.then(function (response) {
try {
$timeout(function () {
saveAs(response.data, "mypdf.pdf");
});
} catch (ex) {
console.log(ex);
}
}, function (response) {
// error
return $q.reject(response.data);
});
})https://stackoverflow.com/questions/27147536
复制相似问题