uploadAudio.vue 3.38 KB
Newer Older
1
<template>
qzhxx's avatar
qzhxx committed
2
<div>
qzhxx's avatar
qzhxx committed
3
  <!-- {{fileList}} -->
4
  <el-upload
qzhxx's avatar
qzhxx committed
5
    ref="upload"
qzhxx's avatar
qzhxx committed
6
    accept=".mp3,.aac,.wma,.rm,.flac,.ogg,"
qzhxx's avatar
qzhxx committed
7 8 9 10 11 12
    :multiple="true"
    :http-request="uploadFile"
    :file-list="fileList"
    action
    :auto-upload="false"
    :before-upload="beforeAvatarUpload"
qzhxx's avatar
qzhxx committed
13
    :on-remove="handleRemove"
qzhxx's avatar
qzhxx committed
14 15
  >
    <i class="el-icon-plus fileUpload"></i>
liqin's avatar
liqin committed
16

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

21 22 23
</template>

<script>
qzhxx's avatar
qzhxx committed
24
export default {
乐宝呗666's avatar
乐宝呗666 committed
25 26 27 28 29 30
  props:{
    fileList:{
      type:Array,
      default:[]
    },
  },
qzhxx's avatar
qzhxx committed
31 32
  data() {
    return {
qzhxx's avatar
qzhxx committed
33
      fullscreenLoading: false,
34
      loading:false,
乐宝呗666's avatar
乐宝呗666 committed
35
      filedata: [],
qzhxx's avatar
qzhxx committed
36 37 38
      fileArr: [],
    };
  },
乐宝呗666's avatar
乐宝呗666 committed
39
  mounted(){
qzhxx's avatar
qzhxx committed
40
    // console.log("uploadAudio",this.fileList)
乐宝呗666's avatar
乐宝呗666 committed
41
  },
qzhxx's avatar
qzhxx committed
42
  methods: {
qzhxx's avatar
qzhxx committed
43 44 45 46 47
     handleRemove(file, fileList) {
        // console.log(fileList,fileList.map(item=>item.id),'1111')
        // return this.$confirm(`确定移除 ${ file.name }?`);
        this.$emit('audioList',fileList.map(item=>item.id))
      },
乐宝呗666's avatar
乐宝呗666 committed
48
    submitUpload() {
qzhxx's avatar
qzhxx committed
49 50 51 52 53 54 55
       const loading = this.$loading({
          lock: true,
          text: '上传中,请稍候。。。',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
      // this.loading = true
qzhxx's avatar
qzhxx committed
56 57
        this.filedata = new FormData()  //  用FormData存放上传文件
        this.$refs.upload.submit()  // 会循环调用uploadFile方法,多个文件调用多次
乐宝呗666's avatar
乐宝呗666 committed
58
        let _this = this;
qzhxx's avatar
qzhxx committed
59 60 61 62 63 64 65 66 67 68 69 70
      _this
        .$https(
          {
            method: "post",
            url: "file/audio/upload",
            authType: this.backToken
          },
          this.filedata
        )
        .then(res => {
          let resData = res.data;
          console.log(res)
qzhxx's avatar
qzhxx committed
71 72
           loading.close();
          //  this.loading = false
qzhxx's avatar
qzhxx committed
73
          if (resData.resultCode == "200") {
74
            _this.$message.success('上传成功!')
qzhxx's avatar
qzhxx committed
75
            const data = resData.data.fileList
乐宝呗666's avatar
乐宝呗666 committed
76 77 78 79 80 81
            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
82 83 84 85 86 87 88 89 90
          } 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
91

92
    },
qzhxx's avatar
qzhxx committed
93 94
    uploadFile(file) {
         this.filedata.append('file', file.file)
95
    },
qzhxx's avatar
qzhxx committed
96
    beforeAvatarUpload(file) {
liqin's avatar
liqin committed
97
      const isLt5M = file.size / 1024 / 1024 / 1024 < 1;
qzhxx's avatar
qzhxx committed
98
      if (!isLt5M) {
liqin's avatar
liqin committed
99
        this.$message.error("上传文件大小不能超过 1GB!");
100
      }
qzhxx's avatar
qzhxx committed
101
      return isLt5M;
qzhxx's avatar
qzhxx committed
102 103 104 105 106 107 108 109 110 111 112

      //  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;

113 114
    }
  }
qzhxx's avatar
qzhxx committed
115
};
116 117 118
</script>

<style>
119
.el-upload .fileUpload {
qzhxx's avatar
qzhxx committed
120 121
  width: 148px;
  height: 148px;
122
  line-height: 148px!important;
qzhxx's avatar
qzhxx committed
123 124 125 126 127
  margin: 0 auto;
  border: 1px dashed #c0ccda;
  font-size: 24px;
  font: #ccc;
}
liqin's avatar
liqin committed
128
</style>