[英]Open PDF blob from Angular Component
在我的Angular應用程序中,單擊按鈕時需要打開PDF。
我首先遵循了這篇文章給出的指導,所以現在我有這兩種方法:
在調用組件中
public PrepareDownload(){
this._certificateService
.GetPdf(args)
.subscribe(
result => {
this.BlobUri = URL.createObjectURL(result);
}
}
public Download() {
window.open(this.BlobUri);
}
在服務中
public GetPdf() : any {
var headers = new Headers();
headers.append("Content-Type", "application/json");
var payload = {id:215};
return this._http.post(this.requestUri,
JSON.stringify(payload),
{ headers: headers,
responseType: ResponseContentType.Blob })
.map((response: Response) =>
{
return new Blob([response.blob()], { type: 'application/pdf' });
});
}
GetPdf的響應包含一個類似於以下[ 37,80,68,70,45,49,46,...]
的數組。 不幸的是,當調用下載方法時,結果似乎是無效的pdf。 通過firefox下載文件並將其重命名為txt文件時,我可以看到內容不是二進制的,而是字符串的完整字節數組。
如@David Siro的評論所指出,該服務必須進行修改。 現在,請求返回一個流而不是字節數組,可以按照問題中的描述進行處理。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.