【问题标题】:Angular 5: Document Download returning "Failed to load PDF"Angular 5:文档下载返回“加载 PDF 失败”
【发布时间】: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


【解决方案1】:

只需构造一个指向服务端点的锚标记

<a [href]="constructServiceEndPoint()" target="_blank">Download</a>

并让函数constructServiceEndPoint 返回一个代表终点的字符串。确保您的服务设置文件名标头。

【讨论】:

    猜你喜欢
    • 2019-05-31
    • 2015-07-09
    • 2014-06-05
    • 1970-01-01
    • 2018-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多