我从服务器端收到一个字节数组,并已成功将其转换为 blob。但是,当我尝试下载它时,它显示文件已损坏。以下是我的代码 -
// In client side controller
this.contractsService.downloadPdf(id)
.then((result) => {
var blob = new Blob([result], { type: "application/pdf" });
var link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = "testing.pdf";
link.click();
});
Run Code Online (Sandbox Code Playgroud)
和,
// In client side service
private headers = new HttpHeaders({ 'Content-Type': 'application/json' });
downloadPdf(id: number) {
return this.http.get(this.apiRoutes.download + "/" + id, { headers: this.headers })
.map((res: any) => res)
.toPromise();
}
Run Code Online (Sandbox Code Playgroud)
任何形式的帮助将不胜感激。谢谢你。