[英]How can I convert a pdf file in a Blob type in Angular 7?
我的項目需要生成一個pdf報告,並且我正在使用pdfcrowd( https://pdfcrowd.com )來執行此操作。
我的方法很簡單,並且從后端(Angular 7)到后端(.Net Core 2.1)的調用工作正常,但是,當我在前端收到pdf時,出現以下錯誤。
回報:200-好
錯誤:語法錯誤:JSON中的意外令牌%在JSON.parse的位置0
我的服務
GetReportPrint(e: PrintModel) {
let token = localStorage.getItem('token');
let body = JSON.stringify(e);
let headers = new HttpHeaders({
'Content-type': 'application/json',
'Authorization': token
});
return this._http.post<any>(
this.API + 'Report', body, {
headers: headers,
responseType: 'blob'
})
.pipe(map((response) => {
let file = response.blob();
var blob = new Blob([file], {
type: 'application/pdf'
});
FileSaver.saveAs(blob, "tes");
}))
}
您在解析json時遇到問題,這是您的錯誤所在。
我建議使用GET下載文件而不是POST,請嘗試以下操作:
this.http.get<any>(url, {responseType: 'blob'}).pipe(..);
嘗試以下操作:做response.text()而不是response.blob()。
.pipe(map((response) => {
let file = response.text();
var blob = new Blob([file], {
type: 'application/pdf'
});
FileSaver.saveAs(blob, "test.pdf");
}))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.