angular - Angular 4:将响应从 API 转换为 PDF
问题描述
大家好,我在尝试从 API 转换响应时遇到问题,出现错误
请求正文既不是 blob 也不是数组缓冲区
这是我的代码
应用服务.ts
downloadPDF(id_booking): any {
let headers = new Headers();
this.createAuthorizationHeader(headers);
return this.http.post('https://localhost:1210/v1/user/booking/eticket/'+id_booking, {responseType: ResponseContentType.Blob}, {headers: headers}).map(
(res) => {
console.log(res)
return new Blob([res.blob()], { type: 'application/pdf' })
})
}
pdf.component.ts
this.appService.downloadPDF(booking._id).subscribe(
(res) => {
saveAs(res, "myPDF.pdf");
var fileURL = URL.createObjectURL(res);
window.open(fileURL);
}
解决方案
您的后端当前正在发送一个字符串。
假设您使用的是快递,而不是
res.send(arr)
你应该使用
res.send(buffer);
这将确保发送二进制数据
推荐阅读
- ms-access - 更新后端 SharePoint 列表时的 MS Access 保留错误 (-1517)
- x86 - x86 MSR 名称的 IA32 与 MSR 前缀
- java - Java 用 str.repeat 重复字符串
- excel - 索引匹配以匹配两个日期之间的日期
- java - Java Selenium 测试:如何从 CSV 读取特定的行和/或行以将数据输入到 TestNG 测试
- patch - 如何重命名被子补丁中的目录?
- javascript - NodeJS:确保不会为多个用户的项目分配相同的 ID 号
- javascript - 从视频反应中拍摄快照
- python - 我需要用python从一个减去另一个的范围数字中得到结果
- azure - Azure Key Vault 配置 - 间歇性“未经授权”响应