我正在尝试从一个给定documentId的API下载一个pdf文件。然而,当文档打开时显示“加载PDF失败”我也检查了chrome开发工具,并且没有对API进行网络调用,有人能给我指出正确的方向吗?
downloadDocument(document) {
// this.documentDownload.emit(document.attachmentId);
this.documentUrl = this/is/a/api/url
const blob = new Blob([this.documentUrl], {type: 'application/pdf' }); //octet-stream
const url = window.URL.createObjectURL(blob);
if (window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(blob, url.split(':')[1] + '.pdf');
return;
}
const a = window.document.createElement('a');
a.href = url;
a.download = document.filename;
a.click();
setTimeout(function() {
window.URL.revokeObjectURL(url); }, 100);
// return url;
}
}发布于 2019-04-16 11:24:18
只需构造一个指向服务端点的锚标记
<a [href]="constructServiceEndPoint()" target="_blank">Download</a>并让函数constructServiceEndPoint返回一个表示终点的字符串。请确保您的服务设置了文件名标题。
https://stackoverflow.com/questions/49280644
复制相似问题