uploadDatum.vue 2.68 KB
Newer Older
1
<template>
乐宝呗666's avatar
乐宝呗666 committed
2 3 4
  <div>
    <el-upload
      ref="upload"
qzhxx's avatar
qzhxx committed
5
      accept=".mp4,.flv,.mpeg,.mpg,.word,.pdf,.ppt,.jpg,.jpeg,.png.JPG,.JPEG,.PNG"
乐宝呗666's avatar
乐宝呗666 committed
6 7 8 9 10 11 12 13 14 15 16 17 18
      :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"
19
      v-loading="loading"
乐宝呗666's avatar
乐宝呗666 committed
20 21 22 23
      @click="submitUpload"
      >上传到服务器</el-button
    >
  </div>
24 25 26
</template>

<script>
qzhxx's avatar
qzhxx committed
27
export default {
乐宝呗666's avatar
乐宝呗666 committed
28 29 30 31 32 33
  props:{
    fileList:{
      type:Array,
      default:[]
    },
  },
qzhxx's avatar
qzhxx committed
34 35
  data() {
    return {
qzhxx's avatar
qzhxx committed
36
      fullscreenLoading: false,
37
      loading:false,
乐宝呗666's avatar
乐宝呗666 committed
38
      filedata: [],
qzhxx's avatar
qzhxx committed
39 40 41
      fileArr: [],
    };
  },
乐宝呗666's avatar
乐宝呗666 committed
42 43
  mounted(){
  },
qzhxx's avatar
qzhxx committed
44
  methods: {
乐宝呗666's avatar
乐宝呗666 committed
45
    submitUpload() {
qzhxx's avatar
qzhxx committed
46 47 48 49 50 51 52
        const loading = this.$loading({
          lock: true,
          text: '上传中,请稍候。。。',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
      // this.loading = true
乐宝呗666's avatar
乐宝呗666 committed
53 54
      this.filedata = new FormData(); //  用FormData存放上传文件
      this.$refs.upload.submit(); // 会循环调用uploadFile方法,多个文件调用多次
qzhxx's avatar
qzhxx committed
55

乐宝呗666's avatar
乐宝呗666 committed
56
      let _this = this;
qzhxx's avatar
qzhxx committed
57 58 59 60 61
      _this
        .$https(
          {
            method: "post",
            url: "file/datum/upload",
乐宝呗666's avatar
乐宝呗666 committed
62
            authType: this.backToken,
qzhxx's avatar
qzhxx committed
63 64 65
          },
          this.filedata
        )
乐宝呗666's avatar
乐宝呗666 committed
66
        .then((res) => {
qzhxx's avatar
qzhxx committed
67
          // this.loading = false
qzhxx's avatar
qzhxx committed
68 69
          let resData = res.data;
          if (resData.resultCode == "200") {
qzhxx's avatar
qzhxx committed
70
            loading.close();
71
            _this.$message.success('上传成功!')
乐宝呗666's avatar
乐宝呗666 committed
72 73 74 75 76 77 78
            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
79 80 81 82
          } else {
            _this.$message.error(resData.msg || resData.message);
          }
        })
乐宝呗666's avatar
乐宝呗666 committed
83
        .catch((err) => {
qzhxx's avatar
qzhxx committed
84 85 86
          console.log(err);
          _this.$message.error(err.msg || err.message);
        });
87
    },
qzhxx's avatar
qzhxx committed
88
    uploadFile(file) {
乐宝呗666's avatar
乐宝呗666 committed
89
      this.filedata.append("file", file.file);
90
    },
qzhxx's avatar
qzhxx committed
91
    beforeAvatarUpload(file) {
liqin's avatar
liqin committed
92
      const isLt10G = file.size / 1024 / 1024 / 1024 < 10;
liqin's avatar
liqin committed
93 94
      if (!isLt10G) {
        this.$message.error("上传文件大小不能超过 10GB!");
95
      }
liqin's avatar
liqin committed
96
      return isLt10G;
97 98
    }
  }
qzhxx's avatar
qzhxx committed
99
};
100 101 102
</script>

<style>
103
.el-upload .fileUpload {
qzhxx's avatar
qzhxx committed
104 105
  width: 148px;
  height: 148px;
106
  line-height: 148px!important;
qzhxx's avatar
qzhxx committed
107 108 109 110 111
  margin: 0 auto;
  border: 1px dashed #c0ccda;
  font-size: 24px;
  font: #ccc;
}
liqin's avatar
liqin committed
112
</style>