【问题标题】:Angular Application: How to change the name of a pdf file when downloaded from an API?Angular Application:从 API 下载时如何更改 pdf 文件的名称?
【发布时间】:2019-09-18 00:26:22
【问题描述】:

在我的 Angular 7 应用程序中,

我有一个 API,当调用它时,它会为用户下载一个 PDF。一个问题是,当下载 PDF 时,名称显示为“[object Blob]”。我怎样才能使它与其他内容相同,例如“确认 PDF”?

在我的服务文件中:

  getPdfCal(payload) {
    return this.http.get(environment.apiUrl + '/report/getPdfCal',
    { headers: this.getSearchApiHeaders(payload), responseType: 'blob' });
  }

在我的组件 TS 文件中:

  getPdf(transactionNbr) {
    if(transactionNbr !== null) {
    const transactionNumberString = transactionNbr+'';
    const payload = { Id: this.IdHeader, Yr: this.Year, transactionNbr: transactionNumberString }
    this.studentService.getPdfCal(payload).subscribe((response: any) => {

    let dataType = "application/pdf";
    let binaryData = [];

    binaryData.push(response);

    let downloadLink = document.createElement('a');

    downloadLink.href = window.URL.createObjectURL(new Blob(binaryData, { type: dataType }));
    if (response)
      downloadLink.setAttribute('download', response);
    document.body.appendChild(downloadLink);
    downloadLink.click();

  })

 }
}

这是从 API 下载文件时的屏幕截图

【问题讨论】:

  • 您将download 属性设置为response。尝试将其设置为另一个名称:)

标签: angular api pdf angular7


【解决方案1】:

download 属性将设置下载文件的文件名。

downloadLink.download = 'hello.pdf'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    • 2017-01-25
    • 2020-03-05
    • 1970-01-01
    • 2011-02-06
    • 2020-11-24
    相关资源
    最近更新 更多