如何以angular 2下载pdf文件 [英] How to download pdf file in angular 2
本文介绍了如何以angular 2下载pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是在我下载 Pdf 文件时以 angular 2 下载 pdf 文件的功能,它是损坏的文件.那么我该如何解决这个问题.它像这样显示损坏的数据.%PDF-1.5%↵66 0 目标<>结束对象
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);
});
}
推荐答案
我们需要这个包file-saver,你像"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屋!
查看全文