国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

element-ui多文件上傳的實(shí)現(xiàn)示例

2024-05-06 15:40:50
字體:
供稿:網(wǎng)友

上傳方案一:

先將文件上傳到七牛,再將七牛上傳返回的文件訪問路徑上傳到服務(wù)器

<div class="upload-music-container">  <el-upload   class="upload-music"   ref="upload"   action="http://up-z2.qiniup.com/"   :data="{token:uploadToken}"   multiple   accept=".mp3"   :before-upload="uploadBefore"   :on-change="uploadChange"   :on-success="uploadSuccess"   :on-error="uploadError">   <el-button size="small" type="primary">選取文件</el-button>   <div slot="tip" class="el-upload__tip">僅支持上傳mp3文件,文件大小不超過500M</div>  </el-upload>  <el-button size="small" type="success" @click="submitUpload">上傳到服務(wù)器</el-button></div> export default {  name: 'uploadMusic',  data() {   return {    headers: {},    uploadToken: null,    canUploadMore: true,    fileList: null,   }  },  created() {   this.headers = {}   //此處需要與server約定具體的header   this.getUploadToken()  },  methods: {   //獲取上傳七牛token憑證   getUploadToken() {    this.$http.get('xxxxxxx', {headers: this.headers}).then(response => {     if (response.data.status == 200) {      let resp = response.data.data      this.uploadToken = resp.token     } else {      this.$message({       message: '獲取憑證失敗,請重試',       type: 'error'      })     }    })   },   //獲取音頻文件時長   getVideoPlayTime(file, fileList) {    let self = this    //獲取錄音時長    try {     let url = URL.createObjectURL(file.raw);     //經(jīng)測試,發(fā)現(xiàn)audio也可獲取視頻的時長     let audioElement = new Audio(url);     let duration;     audioElement.addEventListener("loadedmetadata", function (_event) {      duration = audioElement.duration;      file.duration = duration      self.fileList = fileList     });    } catch (e) {     console.log(e)    }   },   //校驗上傳文件大小   uploadChange(file, fileList) {    this.fileList = fileList    let totalSize = 0    for (let file of fileList) {     totalSize += file.raw.size    }    if (totalSize > 500 * 1024 * 1024) {     this.canUploadMore = false     this.$message({      message: '上傳文件不能不超過500M',      type: 'warn'     })    } else {     this.canUploadMore = true    }   },   uploadBefore(file) {    if (this.canUploadMore) {     return true    }    return false   },   //上傳成功   uploadSuccess(response, file, fileList) {    this.getVideoPlayTime(file, fileList)   },   //上傳失敗   uploadError(err, file, fileList) {    console.log(err)   },   //上傳服務(wù)器數(shù)據(jù)格式化   getUploadMusicList() {    let musicList = []    for (let file of this.fileList) {     if (file.response && file.response.key) {      musicList.push({       "play_time": file.duration, //播放時長       "size": file.size/1024,   //文件大小 單位 kb       "song_name": file.name,   //歌曲名       "voice_url": "xxxx"     //上傳七牛返回的訪問路徑      })     }    }    return musicList   },   //上傳至服務(wù)器   submitUpload() {    let musicList = this.getUploadMusicList()    this.$http.post('xxxxxxxxxx', {music_list: musicList}, {headers: this.headers}).then(response => {     if (response.data.status == 200) {      this.$refs.upload.clearFiles() //上傳成功后清空文件列表      this.$message({       message: '上傳服務(wù)器成功',       type: 'success'      })     } else{      this.$message({       message: '上傳服務(wù)器失敗,請重試',       type: 'error'      })     }    }).catch(err => {     this.$message({      message: '上傳服務(wù)器失敗,請重試',      type: 'error'     })    })   },  } }            
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 津南区| 惠州市| 班戈县| 东源县| 永靖县| 辰溪县| 涞源县| 海淀区| 岳阳县| 滕州市| 巴中市| 田林县| 新竹县| 岑溪市| 遵化市| 微博| 桃园县| 锡林郭勒盟| 化州市| 宜丰县| 永济市| 神木县| 高州市| 浪卡子县| 灵宝市| 万安县| 平昌县| 商南县| 额尔古纳市| 那坡县| 开江县| 察隅县| 武威市| 辽源市| 土默特右旗| 宜丰县| 绵阳市| 布尔津县| 仁寿县| 雷州市| 章丘市|