uploadAudio.vue 2.42 KB
<template>
<div>
<!-- accept="application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" -->
  <!-- :show-file-list="false" -->
  <!-- :file-list="fileList" -->
  <!-- action="http://111.203.232.175:8088/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>

</template>

<script>
export default {
  props:{
    fileList:{
      type:Array,
      default:[]
    },
  },
  data() {
    return {
      filedata: [],
      fileArr: [],
    };
  },
  mounted(){
    console.log("uploadAudio",this.fileList)
  },
  methods: {
    submitUpload() {
        this.filedata = new FormData()  //  用FormData存放上传文件
        this.$refs.upload.submit()  // 会循环调用uploadFile方法,多个文件调用多次
        let _this = this;
      _this
        .$https(
          {
            method: "post",
            url: "file/audio/upload",
            authType: this.backToken
          },
          this.filedata
        )
        .then(res => {
          let resData = res.data;
          console.log(res)

          if (resData.resultCode == "200") {
            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('audioList', [...newArray,...editArray])
          } else {
            _this.$message.error(resData.msg || resData.message);
          }
        })
        .catch(err => {
          console.log(err);
          _this.$message.error(err.msg || err.message);
        });


    },
    uploadFile(file) {
         this.filedata.append('file', file.file)
    },
    beforeAvatarUpload(file) {
      const isLt5M = file.size / 1024 / 1024 / 1024 < 1;
      if (!isLt5M) {
        this.$message.error("上传文件大小不能超过 1GB!");
      }
      return isLt5M;
    }
  }
};
</script>

<style>
.fileUpload {
  width: 148px;
  height: 148px;
  line-height: 148px;
  margin: 0 auto;
  border: 1px dashed #c0ccda;
  font-size: 24px;
  font: #ccc;
}
</style>