0


文件预览功能/文件流前端展示

第一种方式:

文件预览展示

转换为 Blob对象

  // 预览图片 或者pdf格式文件
  getViewImg(id: string) {
    return this.http.get(`/workflow/attachment/viewImg/${id}`, {
      observe: 'response',
      responseType: 'blob'
    });
  }
    // 预览word文件或Excel文件
  getViewFile(id: string) {
    return this.http.get(`/workflow/attachment/view/${id}`, {
      observe: 'response',
      responseType: 'blob'
    })
  }

预览实现ts代码

  // 附件预览
  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="预览"
  [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>

第二种方式:

简单粗暴

展示:

//1、请求接口 请求设置responseType
axios.get(url,{resonseType:'blob'})

//2.根据返回的值创建一个Blob对象, 以pdf文件为例
let blob = new Blob([result],{
       type: "application/pdf;chartset=UTF-8"
})

//3.window.URL.createObjectURL创建一个url连接
let blob = new Blob([result],{
       type: "application/pdf;chartset=UTF-8"
})
let url = window.URL.createObjectURL(blob)

//4.在线预览
//可以用iframe预览,url的值为 window.URL.createObjectURL(blob),或者直接打开window.open(url)

打印:

//1.创建iframe标签
const iframe = document.createElement('iframe')

//2.属性相关
iframe.className = 'tmp-pdf';
iframe.style.display = 'none'
// blobUrl 像在线预览1,2,3这样得来的url
iframe.src = blobUrl

//3.创建好的iframe添加到页面body
document.body.appendChild(iframe)

//4.执行打印方法,并移除iframe
setTimeout(function() {
   iframe.contentWindow.print()
   document.body.removechild(iframe)
}, 100)
标签: java 开发语言

本文转载自: https://blog.csdn.net/b0303/article/details/130430592
版权归原作者 前进的探索者 所有, 如有侵权,请联系我们删除。

“文件预览功能/文件流前端展示”的评论:

还没有评论