如何在Angular 2中下载PDF文件 [英] How to download pdf file in angular 2
本文介绍了如何在Angular 2中下载PDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我下载Pdf文件已损坏的文件时,此功能可下载角度为2的pdf文件.那么我该如何解决这个问题. 它显示像这样的损坏的数据. %PDF-1.5 % ↵660 obj <> endobj
This is function to download pdf file in angular 2 when I am downloading Pdf file it is corrupted file. So how do I solve this problem. It is displaying corrupted data like this. %PDF-1.5 %���� ↵66 0 obj <> endobj
downloadFile() {
//this.http.get('https://contactsapi.apispark.net/v1/companies/').subscribe(
this.http.get('assets/Files/booking.pdf').subscribe(
(response: any) => {
console.log(response);
let parsedResponse =(response)
var blob = new Blob([response], {type: 'application/pdf'});
var filename = 'booking.pdf';
saveAs(blob, filename);
});
}
推荐答案
我们需要此软件包文件保护程序,您可以像"npm install file-saver --save"那样安装,然后可以尝试像这样
we need this package file-saver,you install like "npm install file-saver --save", then you can try like this
public downloadCSVFile(){
this.downloadPdf().subscribe(
(res) => {
saveAs(res,'test.pdf')
}
);
}
public downloadPdf(): any {
let url='your url'
let headers = new Headers();
headers.append('Authorization', 'JWT ' + localStorage.getItem('id_token'));
return this.http.get(url,{ headers: headers,responseType: ResponseContentType.Blob }).map(
(res) => {
return new Blob([res.blob()], { type: 'application/pdf' })
})
}
这篇关于如何在Angular 2中下载PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文