我正在尝试从给定 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;
}
}
只需构造一个指向服务端点的锚标记
<a [href]="constructServiceEndPoint()" target="_blank">Download</a>
并让函数 constructServiceEndPoint 返回一个表示端点的字符串。确保您的服务设置文件名标头。