【发布时间】: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();
})
}
}
【问题讨论】:
-
您将
download属性设置为response。尝试将其设置为另一个名称:)