uploadDatum.vue 2.33 KB
Newer Older
1
<template>
乐宝呗666's avatar
乐宝呗666 committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
  <div>
    <el-upload
      ref="upload"
      :multiple="true"
      :http-request="uploadFile"
      :file-list="fileList"
      action
      :auto-upload="false"
      :before-upload="beforeAvatarUpload"
    >
      <i class="el-icon-plus fileUpload"></i>
    </el-upload>
    <el-button
      style="margin-left: 10px"
      size="small"
      type="success"
18
      v-loading="loading"
乐宝呗666's avatar
乐宝呗666 committed
19 20 21 22
      @click="submitUpload"
      >上传到服务器</el-button
    >
  </div>
23 24 25
</template>

<script>
qzhxx's avatar
qzhxx committed
26
export default {
乐宝呗666's avatar
乐宝呗666 committed
27 28 29 30 31 32
  props:{
    fileList:{
      type:Array,
      default:[]
    },
  },
qzhxx's avatar
qzhxx committed
33 34
  data() {
    return {
35
      loading:false,
乐宝呗666's avatar
乐宝呗666 committed
36
      filedata: [],
qzhxx's avatar
qzhxx committed
37 38 39
      fileArr: [],
    };
  },
乐宝呗666's avatar
乐宝呗666 committed
40 41
  mounted(){
  },
qzhxx's avatar
qzhxx committed
42
  methods: {
乐宝呗666's avatar
乐宝呗666 committed
43
    submitUpload() {
44
      this.loading = true
乐宝呗666's avatar
乐宝呗666 committed
45 46
      this.filedata = new FormData(); //  用FormData存放上传文件
      this.$refs.upload.submit(); // 会循环调用uploadFile方法,多个文件调用多次
qzhxx's avatar
qzhxx committed
47

乐宝呗666's avatar
乐宝呗666 committed
48
      let _this = this;
qzhxx's avatar
qzhxx committed
49 50 51 52 53
      _this
        .$https(
          {
            method: "post",
            url: "file/datum/upload",
乐宝呗666's avatar
乐宝呗666 committed
54
            authType: this.backToken,
qzhxx's avatar
qzhxx committed
55 56 57
          },
          this.filedata
        )
乐宝呗666's avatar
乐宝呗666 committed
58
        .then((res) => {
59
          this.loading = false
qzhxx's avatar
qzhxx committed
60 61
          let resData = res.data;
          if (resData.resultCode == "200") {
62
            _this.$message.success('上传成功!')
乐宝呗666's avatar
乐宝呗666 committed
63 64 65 66 67 68 69
            const data = resData.data.fileList;
            let newArray = data.map((item) => item.id);
            let editArray =[]
            if(this.fileList.length){
              editArray = this.fileList.map(item=>item.id)
            }
            this.$emit("datumList", [...newArray,...editArray]);
qzhxx's avatar
qzhxx committed
70 71 72 73
          } else {
            _this.$message.error(resData.msg || resData.message);
          }
        })
乐宝呗666's avatar
乐宝呗666 committed
74
        .catch((err) => {
qzhxx's avatar
qzhxx committed
75 76 77
          console.log(err);
          _this.$message.error(err.msg || err.message);
        });
78
    },
qzhxx's avatar
qzhxx committed
79
    uploadFile(file) {
乐宝呗666's avatar
乐宝呗666 committed
80
      this.filedata.append("file", file.file);
81
    },
qzhxx's avatar
qzhxx committed
82
    beforeAvatarUpload(file) {
liqin's avatar
liqin committed
83
      const isLt10G = file.size / 1024 / 1024 / 1024 < 10;
liqin's avatar
liqin committed
84 85
      if (!isLt10G) {
        this.$message.error("上传文件大小不能超过 10GB!");
86
      }
liqin's avatar
liqin committed
87
      return isLt10G;
88 89
    }
  }
qzhxx's avatar
qzhxx committed
90
};
91 92 93
</script>

<style>
94
.el-upload .fileUpload {
qzhxx's avatar
qzhxx committed
95 96
  width: 148px;
  height: 148px;
97
  line-height: 148px!important;
qzhxx's avatar
qzhxx committed
98 99 100 101 102
  margin: 0 auto;
  border: 1px dashed #c0ccda;
  font-size: 24px;
  font: #ccc;
}
liqin's avatar
liqin committed
103
</style>