我有位元組陣列,我想下載沒有任何庫的 pdf,例如檔案保護程式。
服務.ts
return this.http.get(`${this.invoiceUrl}/GenerateInvoice`,
{ responseType: "arraybuffer", observe: "response", params }
);
組件.ts
file(byte) {
var byteArray = new Uint8Array(byte);
var a = window.document.createElement('a');
a.href = window.URL.createObjectURL(new Blob([byteArray], { type: 'application/json' }));
a.download = "data.txt";
// Append anchor to body.
document.body.appendChild(a)
a.click();
// Remove anchor from body
document.body.removeChild(a)
}
// download function
this.invoiceService.generateInvoice(id).pipe(
finalize(() => this.loadingFlag = false)
).subscribe(
resp => {
console.log(resp);
this.file(resp.body)
}


如果我寫這個版本是:
如果我儀式:
return this.http.get(`${this.invoiceUrl}/GenerateInvoice`,
{ responseType: "arraybuffer", observe: "response", params }
);
回應是

uj5u.com熱心網友回復:
試試這個它在沒有檔案保護程式的情況下對我有用:
file(data: any) {
const blob = new Blob([data], {type: 'application/pdf'});
let url = URL.createObjectURL(blob);
const pwa = window.open(url);
if (!pwa || pwa.closed || typeof pwa.closed === 'undefined') {
throw error( 'check if your browser block windows');
}
}
或者你可以使用這個例子:
file(data: any) {
const blob = new Blob([data], {type: 'application/pdf'});
let filename = 'myPdfFile';
let url= URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = fileName;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
第一個示例pdf在您的瀏覽器上打開,第二個示例下載檔案。
uj5u.com熱心網友回復:
解決方案:服務檔案中不需要responseType。
downloadPDF(str) {
const linkSource = 'data:application/pdf;base64,' str;
const downloadLink = document.createElement("a");
const fileName = "sample.pdf";
downloadLink.href = linkSource;
downloadLink.download = fileName;
downloadLink.click();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/477271.html
標籤:javascript 有角度的 pdf 下载 角11
