uploadDatum.vue 2.97 KB
Newer Older
1
<template>
乐宝呗666's avatar
乐宝呗666 committed
2 3 4
  <div>
    <el-upload
      ref="upload"
liqin's avatar
liqin committed
5
      accept=".mp4,.flv,.mpeg,.mpg,.mov,.doc,.docx,.pdf,.ppt,.pptx,.jpg,.jpeg,.png,.bmp,.JPG,.JPEG,.PNG"
乐宝呗666's avatar
乐宝呗666 committed
6 7 8 9 10 11
      :multiple="true"
      :http-request="uploadFile"
      :file-list="fileList"
      action
      :auto-upload="false"
      :before-upload="beforeAvatarUpload"
qzhxx's avatar
qzhxx committed
12
      :on-remove="handleRemove"
乐宝呗666's avatar
乐宝呗666 committed
13 14 15 16 17 18 19
    >
      <i class="el-icon-plus fileUpload"></i>
    </el-upload>
    <el-button
      style="margin-left: 10px"
      size="small"
      type="success"
20
      v-loading="loading"
乐宝呗666's avatar
乐宝呗666 committed
21 22 23 24
      @click="submitUpload"
      >上传到服务器</el-button
    >
  </div>
25 26 27
</template>

<script>
qzhxx's avatar
qzhxx committed
28
export default {
乐宝呗666's avatar
乐宝呗666 committed
29 30 31 32 33 34
  props:{
    fileList:{
      type:Array,
      default:[]
    },
  },
qzhxx's avatar
qzhxx committed
35 36
  data() {
    return {
qzhxx's avatar
qzhxx committed
37
      fullscreenLoading: false,
38
      loading:false,
乐宝呗666's avatar
乐宝呗666 committed
39
      filedata: [],
qzhxx's avatar
qzhxx committed
40 41 42
      fileArr: [],
    };
  },
乐宝呗666's avatar
乐宝呗666 committed
43 44
  mounted(){
  },
qzhxx's avatar
qzhxx committed
45
  methods: {
qzhxx's avatar
qzhxx committed
46 47 48 49 50
     handleRemove(file, fileList) {
        // console.log(fileList,fileList.map(item=>item.id),'1111')
        // return this.$confirm(`确定移除 ${ file.name }?`);
        this.$emit('datumList',fileList.map(item=>item.id))
      },
乐宝呗666's avatar
乐宝呗666 committed
51
    submitUpload() {
qzhxx's avatar
qzhxx committed
52 53 54 55 56 57 58
        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
59 60
      this.filedata = new FormData(); //  用FormData存放上传文件
      this.$refs.upload.submit(); // 会循环调用uploadFile方法,多个文件调用多次
qzhxx's avatar
qzhxx committed
61

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

<style>
109
.el-upload .fileUpload {
qzhxx's avatar
qzhxx committed
110 111
  width: 148px;
  height: 148px;
112
  line-height: 148px!important;
qzhxx's avatar
qzhxx committed
113 114 115 116 117
  margin: 0 auto;
  border: 1px dashed #c0ccda;
  font-size: 24px;
  font: #ccc;
}
liqin's avatar
liqin committed
118
</style>