当前位置: 首页 > news >正文

nz-upload 手动上传 PDF预览

<nz-upload [(nzFileList)]="fileList" [nzBeforeUpload]="beforeUpload" >
   <button nz-button>
       <i nz-icon nzType="upload"></i>
       上传材料
   </button>
</nz-upload>



  /**编辑的对象 */
  editingItem: ReportMaterialDTO = Object.create(null);
  /**上传的文件 */
  fileList: NzUploadFile[] = [];
  /**支持上传文件的后缀 */
  fileSuffix = ['.pptx', '.pdf', '.doc', '.docx'];

手动上传

  /**手动上传 */
  beforeUpload = (file: NzUploadFile): boolean => {
    if (!this.fileSuffix.some(value => file.name.endsWith(value))) {
      this.message.error('上传仅支持ppt、word、pdf格式材料!');
      return false;
    }
    this.fileList = [file];
    return false;
  };

预览、修改

  /**预览 */
  toPreview(item: ReportMaterialDTO) {
    this.systemFile.previewFile(item.materialFile?.id!).subscribe(data => {
      // this.progress.endPercent();
      setTimeout(() => {
        console.log('data',data)
        this.preview(data, item.materialFile?.originalName!);
      }, 800);
    });
  }

  preview(data: HttpResponse<Blob>, name: string) {
    if ('msSaveOrOpenBlob' in window.navigator) {
      // 如果浏览器是旧版的 Internet Explorer(IE),使用 msSaveOrOpenBlob 方法保存或打开文件,并将文件扩展名改为 .pdf
      window.navigator.msSaveOrOpenBlob(data.body, name.slice(0, name.lastIndexOf('.')) + '.pdf');
    } else {
      const extention = name.substring(name.lastIndexOf('.'));
      if (extention === '.jpg' || extention === '.jpeg' || extention === '.png' || extention === '.bmp' || extention === '.gif') {
        // 检查文件扩展名是否为常见的图片格式(如 .jpg, .jpeg, .png, .bmp, .gif)。
        // 如果是图片文件,使用 modalService.info 打开一个模态框,显示图片预览。
        this.modalService.info({
          nzTitle: name,
          nzClassName: 'modal-lg',
          nzBodyStyle: { margin: '0 auto' },
          nzContent: '<img class="modal-img-preview" src="' + data.url + '"/>',
          nzOkText: '关闭'
        });
      } else {
        // 处理其他文件类型(如 PDF 等
        const pdfSrc = window.URL.createObjectURL(data.body!);
        window.open(pdfSrc);
      }
    }
  }

  /**编辑 */
  editData(item: ReportMaterialDTO) {
    this.editingItem = item;
    this.isVisible = true;
    if (item.materialFile) {
      let file: any = { ...item.materialFile };
      file.name = item.materialFile?.originalName;
      this.fileList = [file];
    }
  }

// 异步方法,模拟数据获取
  async uploadFile(formData: FormData): Promise<SystemFileDTO> {
    return new Promise((resolve, reject) => {
      /**先上传材料 再新增 */
      this.systemFile.uploadSingleFile(formData, encodeURIComponent('汇报材料')).subscribe({
        next: file => {
          resolve(file);
        },
        error: (err: any) => {
          reject(err);
        },
        complete: () => {}
      });
    });
  }

  /**弹框确认事件 */
  async handleOk() {
    if (!FormUtil.validateForm(this.materialForm?.form)) {
      return;
    }
    if (this.fileList.length == 0) {
      this.message.warning('请上传材料!');
      return;
    }

    this.isOkLoading = true;
    //有原始名称就代表不是上传的,而是编辑时回显的
    if (!this.fileList[0].originalName) {
      console.log('fileList',this.fileList);
      
      const formData: FormData = new FormData();
      const file: any = this.fileList[0];
      formData.append('file', file, file.name);
      console.log('formData',formData)
      this.editingItem.materialFile = await this.uploadFile(formData);
    }
    if (this.editingItem.id) {
      /**修改 */
      this.service.update(this.editingItem.id, this.editingItem).subscribe({
        next: res => {
          this.loadTableData();
        },
        complete: () => {
          this.isVisible = false;
          this.isOkLoading = false;
        }
      });
    } else {
      /**新增 */
      this.service.create(this.editingItem).subscribe({
        next: res => {
          this.loadTableData();
        },
        complete: () => {
          this.isVisible = false;
          this.isOkLoading = false;
        }
      });
    }
  }

相关文章:

  • 树莓派简单操作系统制作之四:关于异常等级
  • 51c嵌入式~电路~合集13
  • 深度学习-135-LangGraph之应用实例(四)构建RAG问答系统同时对文档进行元数据增强
  • 游戏开发微信小程序--工具箱之父
  • 基于javaweb的SSM+Maven鲜花商城管理系统设计和实现(源码+文档+部署讲解)
  • Qt for Android下QMessageBox背景黑色、文字点击闪烁
  • 深入探讨Ceph:分布式存储架构的未来
  • 类和对象——拷贝对象时的一些编译器优化
  • AI绘画软件Stable Diffusion详解教程(3):Windows系统本地化部署操作方法(通用版)
  • Mybatis的分页插件
  • 视频级虚拟试衣技术在淘宝的产品化实践
  • 【Linux基础】Linux下的C编程指南
  • HAL库之是stm32cubemx安装
  • 定义数组存储3部汽车对象(class1)
  • 解决“ReadTimeoutError:HTTPSConnectionPool”pip安装超时问题
  • win11编译pytorch cuda128版本流程
  • wav格式的音频压缩,WAV 转 MP3 VBR 体积缩减比为 13.5%、多个 MP3 格式音频合并为一个、文件夹存在则删除重建,不存在则直接建立
  • 笔记:大模型Tokens是啥?为啥大模型按Tokens收费?
  • PyQt5入门教程和简单使用
  • Pandas在爬虫中的应用:快速清洗和存储表格数据
  • 技术派|台军首次试射“海马斯”火箭炮,如何压制这种武器?
  • 减重人生|吃得越少越好?比体重秤上的数字,更有意义的是什么?
  • 招商基金总经理徐勇因任期届满离任,“老将”钟文岳回归接棒
  • 世卫大会连续九年拒绝涉台提案
  • 专访|金七猫奖得主:以非遗为舟,在现实题材中疗愈与成长
  • 中美贸易代表会谈后是否已确定下一次会谈?外交部回应