uploadAudio.vue 2.34 KB
Newer Older
1
<template>
qzhxx's avatar
qzhxx committed
2
<div>
3
  <el-upload
qzhxx's avatar
qzhxx committed
4 5 6 7 8 9 10 11 12
    ref="upload"
    :multiple="true"
    :http-request="uploadFile"
    :file-list="fileList"
    action
    :auto-upload="false"
    :before-upload="beforeAvatarUpload"
  >
    <i class="el-icon-plus fileUpload"></i>
liqin's avatar
liqin committed
13

qzhxx's avatar
qzhxx committed
14
  </el-upload>
15
   <el-button style="margin-left: 10px;" size="small" type="success" v-loading="loading" @click="submitUpload">上传到服务器</el-button>
qzhxx's avatar
qzhxx committed
16
</div>
liqin's avatar
liqin committed
17

18 19 20
</template>

<script>
qzhxx's avatar
qzhxx committed
21
export default {
乐宝呗666's avatar
乐宝呗666 committed
22 23 24 25 26 27
  props:{
    fileList:{
      type:Array,
      default:[]
    },
  },
qzhxx's avatar
qzhxx committed
28 29
  data() {
    return {
30
      loading:false,
乐宝呗666's avatar
乐宝呗666 committed
31
      filedata: [],
qzhxx's avatar
qzhxx committed
32 33 34
      fileArr: [],
    };
  },
乐宝呗666's avatar
乐宝呗666 committed
35 36 37
  mounted(){
    console.log("uploadAudio",this.fileList)
  },
qzhxx's avatar
qzhxx committed
38
  methods: {
乐宝呗666's avatar
乐宝呗666 committed
39
    submitUpload() {
40
      this.loading = true
qzhxx's avatar
qzhxx committed
41 42
        this.filedata = new FormData()  //  用FormData存放上传文件
        this.$refs.upload.submit()  // 会循环调用uploadFile方法,多个文件调用多次
乐宝呗666's avatar
乐宝呗666 committed
43
        let _this = this;
qzhxx's avatar
qzhxx committed
44 45 46 47 48 49 50 51 52 53 54 55
      _this
        .$https(
          {
            method: "post",
            url: "file/audio/upload",
            authType: this.backToken
          },
          this.filedata
        )
        .then(res => {
          let resData = res.data;
          console.log(res)
56
           this.loading = false
qzhxx's avatar
qzhxx committed
57
          if (resData.resultCode == "200") {
58
            _this.$message.success('上传成功!')
qzhxx's avatar
qzhxx committed
59
            const data = resData.data.fileList
乐宝呗666's avatar
乐宝呗666 committed
60 61 62 63 64 65
            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])
qzhxx's avatar
qzhxx committed
66 67 68 69 70 71 72 73 74
          } else {
            _this.$message.error(resData.msg || resData.message);
          }
        })
        .catch(err => {
          console.log(err);
          _this.$message.error(err.msg || err.message);
        });

liqin's avatar
liqin committed
75

76
    },
qzhxx's avatar
qzhxx committed
77 78
    uploadFile(file) {
         this.filedata.append('file', file.file)
79
    },
qzhxx's avatar
qzhxx committed
80
    beforeAvatarUpload(file) {
liqin's avatar
liqin committed
81
      const isLt5M = file.size / 1024 / 1024 / 1024 < 1;
qzhxx's avatar
qzhxx committed
82
      if (!isLt5M) {
liqin's avatar
liqin committed
83
        this.$message.error("上传文件大小不能超过 1GB!");
84
      }
qzhxx's avatar
qzhxx committed
85
      return isLt5M;
86 87
    }
  }
qzhxx's avatar
qzhxx committed
88
};
89 90 91
</script>

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