前端浏览器下载文件

 // 下载附件
    Download() {
      console.log(this.formFather.compressFileId, "this.formFather.compressFileId");
      if (this.formFather.compressFileId == null) {
        this.$_Methods.Message.warn("附件还未上传");
      }
      this.$_Api.Basics.Get({
        url: "",
        params: {
          fileId: "",
        },
      }).then(res => { 
//base64 数据流
        this.viewPdfs(res);
      });
    },
    viewPdfs(content) {
      const blob = this.base64ToBlobs(content);
      if (window.navigator && window.navigator.msSaveOrOpenBlob) {
        window.navigator.msSaveOrOpenBlob(blob);
      } else {
        const fileURL = URL.createObjectURL(blob);
        // window.open(fileURL);
        const link = document.createElement("a");
        link.href = fileURL;
        // 设置下载的文件名
        link.download = "123";

        // 触发点击
        document.body.appendChild(link);
        link.click();
        // 清理
        document.body.removeChild(link);
        URL.revokeObjectURL(fileURL);
      }
    },
    base64ToBlobs(code) {
      code = code.replace(/[\n\r]/g, "");
      const raw = window.atob(code);
      const rawLength = raw.length;
      const uInt8Array = new Uint8Array(rawLength);
      for (let i = 0; i < rawLength; ++i) {
        uInt8Array[i] = raw.charCodeAt(i);
      }
//      return new Blob([uInt8Array], { type: "application/pdf" });  根据你上传的文件类型决定
      return new Blob([uInt8Array], { type: `${"application" + "/" + this.formFather.compressType.replace(/\./, "")}` });
    },

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值