【发布时间】:2021-08-20 07:49:14
【问题描述】:
我正在尝试使用这两种方法从 rest api 下载 pdf 文件:
download(): ng.IPromise<Blob> {
return this.$http.get<ArrayBuffer>(this.urls.getUrl())
.then(response => {
return new Blob([response.data], {type: 'application/pdf'});
}
)
}
this.service.download().then( file => {
var a = document.createElement("a"),
url = URL.createObjectURL(file);
open(url)
a.href = url;
a.download = 'invoice';
document.body.appendChild(a);
a.click();
setTimeout(function() {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 0);
}
而且创建的 Blob 的编码似乎存在一些问题。如果我从 Postman 调用此端点并选择“保存响应”然后选择“保存到文件”,我会得到正确的 pdf。当我使用提供的代码时,文件似乎编码不正确(用vim打开的结构通常相似,但符号不同)。
【问题讨论】:
标签: javascript angular rest pdf encoding