[英]Sending pdf file from server to client
我想從服務器發送pdf文件,並在客戶端顯示它。 我使用的是Java的服務器端技術,而使用的是角度6的客戶端。請建議我。 提前致謝。
使用數組緩沖區
服務電話:
在文件服務中:
getFileFromServer(url){
return this.httpService.getUploadedFile(url);
}
在HTTP服務中:
getUploadedFile(url:string){
//just for authentication and file access.
let headers = new HttpHeaders().append("Authorization", "Bearer " + token)
return this.httpClient.get(url, {responseType: 'arraybuffer',headers:headers});
}
在組件中:
使用Blob
Blob參考-----> Blob
在這里this.type ='application / pdf'
例如,用戶單擊查看按鈕:
比(click)="getPdfFile()"
會觸發。
它將調用服務方法來獲取響應作為arraybuffer
getPdfFile() {
this.fileService.getFileFromServer(url).subscribe(responseData => {
//catch response as array buffer
this.showPdfFile(responseData, this.type);
}, error => {
//catch error if any
console.log(error);
});
}
showPdfFile(data: any, type: string) {
var blob = new Blob([data], { type: type });
var url = window.URL.createObjectURL(blob);
// it will open url to new tab.
window.open(url);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.