vue圖片上傳到七牛雲


代碼:

<template>
  <div class="upload-info">
    <div>
      <el-upload
          class="upload-pic"
          :action="domain"
          :data="QiniuData"
          :on-remove="handleRemove"
          :on-error="uploadError"
          :on-success="uploadSuccess"
          :before-remove="beforeRemove"
          :before-upload="beforeAvatarUpload"
          :limit="3"
          multiple
          :on-exceed="handleExceed"
          :file-list="fileList"
        >
        <el-button size="small" type="primary">選擇圖片</el-button>
      </el-upload>
      <div>
        <img class="pic-box" :src="uploadPicUrl" v-if="uploadPicUrl">
      </div>
    </div>
    <div>
      <el-button type="primary" :loading="loading" @click="handleSubmit">提交</el-button>
      <el-button type="info" plain >取消</el-button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loading: false,
      QiniuData: {
        key: "", //圖片名字處理
        token: "" //七牛雲token
      },
      domain: "https://upload-z2.qiniup.com", // 七牛雲的上傳地址(華南區)
      qiniuaddr: "http://xxxx.com", // 七牛雲的圖片外鏈地址
      uploadPicUrl: "", //提交到后台圖片地址
      fileList: []
    };
  },
  mounted() {
    this.getQiniuToken();
  },
  methods: {
    handleRemove(file, fileList) {
      this.uploadPicUrl = "";
    },
    handleExceed(files, fileList) {
      this.$message.warning(
        `當前限制選擇 3 張圖片,如需更換,請刪除上一張圖片在重新選擇!`
      );
    },
    beforeAvatarUpload(file) {
      const isPNG = file.type === "image/png";
      const isJPEG = file.type === "image/jpeg";
      const isJPG = file.type === "image/jpg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isPNG && !isJPEG && !isJPG) {
        this.$message.error("上傳頭像圖片只能是 jpg、png、jpeg 格式!");
        return false;
      }
      if (!isLt2M) {
        this.$message.error("上傳頭像圖片大小不能超過 2MB!");
        return false;
      }
      this.QiniuData.key = `upload_pic_${file.name}`;
    },
    uploadSuccess(response, file, fileList) {
      console.log(fileList);
      this.uploadPicUrl = `${this.qiniuaddr}/${response.key}`;
    },
    uploadError(err, file, fileList) {
      this.$message({
        message: "上傳出錯,請重試!",
        type: "error",
        center: true
      });
    },
    beforeRemove(file, fileList) {
      // return this.$confirm(`確定移除 ${ file.name }?`);
    },
    //提交數據到后台
    handleSubmit() {
      let ajaxData = {
        receipt_img: this.uploadPicUrl //圖片地址
      };
      this.$http.put("/xxx", ajaxData)
        .then(response => {
          let { code, data } = response.data;
          if (code == "0") {
            this.$message({
              message: "提交成功!",
              type: "success",
              center: true
            });
          }
        })
        .catch(error => {
          this.$message({
            message: error.msg,
            type: "error",
            center: true
          });
        });
    },
    //請求后台拿七牛雲token
    getQiniuToken() {
      this.$http.get("/xxx")
        .then(response => {
          let { code, data } = response.data;
          if (code == "0") {
            this.QiniuData.token = data;
          }
        })
        .catch(error => {});
    }
  }
};
</script>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM