【发布时间】:2018-08-23 03:41:32
【问题描述】:
我正在尝试从给定 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;
}
}
【问题讨论】:
-
我没有看到任何 http 调用
-
我看到的所有示例都只使用了一个 blob,我会在 createObjectURL 之前还是之后进行调用。如果我遗漏了什么,请原谅我。对angular还是很陌生~
-
根据我对 Angular、js 和 http 调用的了解,您需要进行 http 调用,否则不会调用
-
是的,我阅读了更多内容,并尝试将其放入。
-
我想我做错了 b/c 它不工作
标签: angular typescript rxjs ngrx