uploadAudio.vue 2.83 KB
Newer Older
1
<template>
qzhxx's avatar
qzhxx committed
2
<div>
3
  <el-upload
qzhxx's avatar
qzhxx committed
4
    ref="upload"
qzhxx's avatar
qzhxx committed
5
    accept=".mp3,.aac,.wma,.rm,.flac,.ogg,"
qzhxx's avatar
qzhxx committed
6 7 8 9 10 11 12 13
    :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
14

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

19 20 21
</template>

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

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

      //  var testmsg = file.name.substring(file.name.lastIndexOf('.')+1)
      //    const extension = (testmsg === 'mp3')||(testmsg === 'aac')||(testmsg === 'wma')||(testmsg === 'rm')||(testmsg === 'flac')||(testmsg === 'ogg')
      //    if(!extension){
      //      this.$message({
      //        message:"上传文件只能是mp3,aac,wma,rm,flac,ogg,格式!",
      //        type:'error'
      //      })
      //    }
      //    return extension;

98 99
    }
  }
qzhxx's avatar
qzhxx committed
100
};
101 102 103
</script>

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