前端 vue上传七牛云 (需后端返回token)

<template>
  <div class="upload-info">
    <el-upload
      style="margin: 20px auto"
      :action="domain"
      :data="QiniuData"
      :on-remove="handleRemove"
      :on-error="uploadError"
      :on-success="uploadSuccess"
      :before-upload="beforeAvatarUpload"
      :on-preview="handlePictureCardPreview"
      :file-list="fileList"
      list-type="picture-card"
      :limit="limit"
    >
      <i class="el-icon-plus"></i>
    
        <!-- <el-button size="small" type="primary">点击上传</el-button> -->
        <div slot="tip" class="el-upload__tip">
          只能上传jpg/png文件,且不超过500kb
        </div>
  
    </el-upload>
    <el-dialog :visible.sync="dialogVisible" style="margin: 5vh auto">
      <img width="100%" :src="dialogImageUrl" alt="" />
    </el-dialog>
  </div>
</template>
<script>
import * as api from "@/api/supervise";
export default {
  props: ["limit", "fileEcho", "isPreview", "disabled"],
  data() {
    return {
      fileList: [],
      loading: false,
      QiniuData: {
        key: "", //图片名字处理
        token: "", //七牛云token
        data: {},
      },
      domain: "http://up-z1.qiniup.com", // 七牛云的上传地址(华东区)
      dialogImageUrl: "",
      dialogVisible: false,
    };
  },
  watch: {
    fileEcho(nv) {
      if (nv) {
        this.fileList = this.fileEcho;
        console.log(this.fileList, "111111");
      }
    },
  },
  mounted() {
    this.getQiniuToken();
  },
  methods: {
    handlePictureCardPreview(file) {
      this.dialogVisible = true;
    },
    handleRemove(file, fileList) {
      this.fileList = fileList;
      this.$emit("getResponse", this.fileList);
    },
    beforeAvatarUpload(file) {
      //图片上传之前的方法
      this.QiniuData.data = file;
      this.QiniuData.key = `${file.name}`;
    },
    uploadSuccess(response, file, fileList) {
      //图片上传成功的方法
      var url = "http://patrol.sonser.cc/" + response.key;
      this.fileList.push({
        name: file.name,
        size: file.size,
        url: url,
      });

      this.$emit("getResponse", this.fileList);
    },
    uploadError(err, file, fileList) {
      //图片上传失败时调用
      this.$message({
        message: "上传出错,请重试!",
        type: "error",
        center: true,
      });
    },
    //请求后台拿七牛云token
    async getQiniuToken() {
      //token
      let uploadtoken = await api.getQiniuToken({});
      this.QiniuData.token = uploadtoken.data;
    },
  },
};
</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值