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

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;
        }
      });
    }
  }


http://www.kler.cn/a/564685.html

相关文章:

  • DeepSeek R1 + 飞书机器人实现AI智能助手
  • Linux中子线程会继承父线程对相关变量的可见性
  • docker-compose部署onlyoffice8.3.0并支持ssl,且支持通过nginx代理,关闭JWT配置
  • 【MySQL学习】关系数据库标准语言SQL
  • SC95F8767的学习——新工程的建立
  • WPF学习之Prism(二)
  • 11特殊函数
  • 【数据结构】二叉树(门槛极低的系统理解)
  • Apache Flink:实时数据流处理的终极武器
  • 关于家用 电视盒子[机顶盒] 的捣鼓日志 2025/2/27
  • 数据预处理实战:缺失值处理与数据标准化
  • 企业并购中SAP系统的三大数据转型挑战以及来如何应对?
  • 李代数(Lie Algebras)与Attention:深度学习中的数学之美
  • 记一次命令行启动springboot项目的问题 java -jar的问题
  • AF3 pair_sequences函数解读
  • MongoDB—(一主、一从、一仲裁)副本集搭建
  • 利用 Windows Terminal 和 SSH Config 简化 Linux 服务器管理
  • DeepSeek-R1-671B大模型满血版私有化部署高可用教程-SparkAi系统集成图文教程
  • python-leetcode-分割等和子集
  • 想转行做春晚那种扭秧歌的机器人,大概要会点什么?