【发布时间】:2016-10-24 08:01:44
【问题描述】:
收到的文件通过$http,我需要将响应数据保存到一个文件中,我使用的是angular-file-saver,收到的文件是pdf docx,但是查看保存的pdf文件在阅读器上什么也看不到,而像PostMan 这样的其他下载器确实保存了它。
负责保存的函数有这样的代码:
function saveFile(data, header){
var extensions = {
"application/pdf": ".pdf",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": ".docx"
}
var file = new Blob([data], {type: header});
FileSaver.saveAs(file, documentsShow.document.name + extensions[header]);
}
data 是从$http 成功检索到的正文数据,header 是从$http 响应中收到的Content-Type
如果需要,$http 代码如下:
$http({
method: "GET",
data: data, //This is sometimes null but it's not relevant for the application
url: "path/to/download"
}).then(function(data){
saveFile(data.data, data.header("Content-Type"));
});
【问题讨论】: