我想从服务器发送pdf文件,并显示在客户端。我使用服务器端技术作为java,客户端作为角6,请推荐我。提前谢谢。
发布于 2018-08-25 05:37:55
使用arraybuffer
服务调用:
在档案服务中:
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引用-> 水滴
在这里,this.type = 'application/pdf‘
例如,用户单击View:
比(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);
}https://stackoverflow.com/questions/52014205
复制相似问题