第一種方式:
文件預(yù)覽展示
轉(zhuǎn)換為 Blob對(duì)象
// 預(yù)覽圖片 或者pdf格式文件
getViewImg(id: string) {
return this.http.get(`/workflow/attachment/viewImg/${id}`, {
observe: 'response',
responseType: 'blob'
});
}
// 預(yù)覽word文件或Excel文件
getViewFile(id: string) {
return this.http.get(`/workflow/attachment/view/${id}`, {
observe: 'response',
responseType: 'blob'
})
}
預(yù)覽實(shí)現(xiàn)ts代碼
// 附件預(yù)覽
handlePreviewClick(item: any) {
if (!this.isPreviewModalVisible) {
this.changeIsSpinning.emit(true);
this.isPreviewModalVisible = true;
this.id = item.id;
this.isImage = this.handleiIsImage(item.attachmentName);
this.isPDF = this.handleiIsPDF(item.attachmentName);
let type = (this.isImage || this.isPDF) ? 'getViewImg' : 'getViewFile';
this.service[type](this.id).subscribe(
async (res: any) => {
if (this.isImage) {
const downloadUrl = window.URL.createObjectURL(res.body);
this.fileHTML = downloadUrl;
} else if (this.isPDF) {
let newBlob = new window.Blob([res.body], { type: 'application/pdf;charset-UTRF-8' })
const downloadUrl = window.URL.createObjectURL(newBlob);
this.fileHTML = downloadUrl;
} else {
this.fileHTML = await res.body.text();
}
this.changeIsSpinning.emit(false);
}, () => {
this.messageService.error('加載失敗');
this.isPreviewModalVisible = false;
this.changeIsSpinning.emit(false);
}
)
}
}
彈窗代碼
<nz-modal *ngIf="fileHTML" [nzStyle]="{ top: '50px' }" [nzWidth]="1100" [nzBodyStyle]="bodyStyle" [nzContent]="modalContent" [(nzVisible)]="isVisible" nzTitle="預(yù)覽"
[nzFooter]="modalFooter" (nzOnCancel)="handleCancel()">
<ng-template #modalContent>
<div *ngIf="!isImage&&!isPDF" [innerHtml]="fileHTML"></div>
<img *ngIf="isImage" [src]="fileHTML" alt="加載失敗" style="max-width:1044px"/>
<iframe *ngIf="isPDF" [src]="fileHTML" alt="加載失敗" style="width:1044px;height: 510px;"></iframe>
</ng-template>
<ng-template #modalFooter>
<button nz-button nzType="primary" (click)="handleOk()">確定</button>
</ng-template>
</nz-modal>
第二種方式:
簡(jiǎn)單粗暴
展示:文章來源:http://www.zghlxwxcb.cn/news/detail-757888.html
//1、請(qǐng)求接口 請(qǐng)求設(shè)置responseType
axios.get(url,{resonseType:'blob'})
//2.根據(jù)返回的值創(chuàng)建一個(gè)Blob對(duì)象, 以pdf文件為例
let blob = new Blob([result],{
type: "application/pdf;chartset=UTF-8"
})
//3.window.URL.createObjectURL創(chuàng)建一個(gè)url連接
let blob = new Blob([result],{
type: "application/pdf;chartset=UTF-8"
})
let url = window.URL.createObjectURL(blob)
//4.在線預(yù)覽
//可以用iframe預(yù)覽,url的值為 window.URL.createObjectURL(blob),或者直接打開window.open(url)
打印:文章來源地址http://www.zghlxwxcb.cn/news/detail-757888.html
//1.創(chuàng)建iframe標(biāo)簽
const iframe = document.createElement('iframe')
//2.屬性相關(guān)
iframe.className = 'tmp-pdf';
iframe.style.display = 'none'
// blobUrl 像在線預(yù)覽1,2,3這樣得來的url
iframe.src = blobUrl
//3.創(chuàng)建好的iframe添加到頁(yè)面body
document.body.appendChild(iframe)
//4.執(zhí)行打印方法,并移除iframe
setTimeout(function() {
iframe.contentWindow.print()
document.body.removechild(iframe)
}, 100)
到了這里,關(guān)于文件預(yù)覽功能/文件流前端展示的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!