uploadDatum.vue 2.4 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 18 19 20 21 22 23 24 25
  <div>
    <!-- accept="application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" -->
    <!-- :show-file-list="false" -->
    <!-- :file-list="fileList" -->
    <!-- action="http://192.168.110.67/mall/file/video/content/upload" -->
    <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"
      @click="submitUpload"
      >上传到服务器</el-button
    >
  </div>
26 27 28
</template>

<script>
qzhxx's avatar
qzhxx committed
29
export default {
乐宝呗666's avatar
乐宝呗666 committed
30 31 32 33 34 35
  props:{
    fileList:{
      type:Array,
      default:[]
    },
  },
qzhxx's avatar
qzhxx committed
36 37
  data() {
    return {
乐宝呗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 46 47
    submitUpload() {
      this.filedata = new FormData(); //  用FormData存放上传文件
      this.$refs.upload.submit(); // 会循环调用uploadFile方法,多个文件调用多次
qzhxx's avatar
qzhxx committed
48

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

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