使用angular $ http下载时文件已损坏 [英] File is corrupted while downloading using angular $http
本文介绍了使用angular $ http下载时文件已损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用angular $ http从服务器下载文件。文件类型可以不同。
我应该设置请求标头以进行身份验证。
下载完成后,文件已损坏!
这是我在客户端保存文件的代码:
I am using angular $http to download file from server. file types can be different. I should set request header in order to authentication. when download finishes, file is corrupted! here is my code in client side to save the file:
getFile: function(file) {
$http({
method: 'GET',
url: 'download' + "/" + file.name,
headers: {
"X-AUTH-TOKEN": "my-token",
Accept: "*/*",
}
}).success(function(data) {
var fileBlob = new Blob([data], {
type: '*/*;charset=utf-8'
});
saveAs(fileBlob, file.name);
}).error(function(err) {
console.log('err', err);
});
}
推荐答案
我终于通过添加解决了这个问题这配置为ajax请求:
I finally solved it by adding this configs to ajax request:
dataType : "binary",
processData : false,
responseType : 'arraybuffer'
并将blob类型更改为
and changing blob type to
application / octet-stream
"application/octet-stream"
这篇关于使用angular $ http下载时文件已损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文