base64 图片转为二进制文件;上传文件相关操作

// base64 图片转为2进制文件
    base64ToBlob (code) {
      let parts = code.split(';base64,')
      let contentType = parts[0].split(':')[1]
      let raw = window.atob(parts[1])
      let rawLength = raw.length
      let uInt8Array = new Uint8Array(rawLength)
      for (let i = 0; i < rawLength; ++i) {
        uInt8Array[i] = raw.charCodeAt(i)
      }
      return new Blob([uInt8Array], {
        type: contentType
      })
    },

导入上传文件

// 请求头: 
  file_xiaodu_daoRuList: {
    url: "url",   // 填入后台接口
    headers: {
      "Content-Type": "application/json;charset=UTF-8",
    },
    method: "file",  // 注意对于上传文件来说method: "file",否则文件可能传不过去
    paramType: "data",
  },



              <a-upload
                name="file"
                :multiple="false"
                :customRequest="uploadFiles"
                :showUploadList="false"
                :beforeUpload="beforeUpload"
                :file-list="fileList"
              >
                <a-button>导入</a-button> 
              </a-upload>


 //上传前文件校验
    beforeUpload(file) {
      const isWord =
        file.type === "application/pdf" ||
        file.type ===
          "application/vnd.openxmlformats-officedocument.wordprocessingml.document" ||
        file.type === "application/msword" ||
        file.type === "application/vnd.ms-excel" ||
        file.type ===
          "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
      const isLt2M = file.size / 1024 / 1024 < 50;
      return new Promise((resolve, reject) => {
        if (!isWord) {
          this.$message.error("请上传doc、docx、pdf、xls、xlsx格式的文件");
          reject(file);
        } else if (!isLt2M) {
          this.$message.error("上传文件必须小于50MB!");
          reject(file);
        } else {
          resolve(file);
        }
      });
    },
// 上传
    uploadFiles(file) {
      const formData = new FormData();
      formData.append("file", file.file);
      this.$http("URL", formData, true).then(
        (res) => {
          console.log(res, 2312);
          if (res.code === 200) {
            this.$message.success(res.message);
          } else {
            this.$message.error(res.message);
            file.status = "error";
          }
        }
      );
      this.getList();
    },

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小杜coding

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值