element-ui多文件上傳的實(shí)現(xiàn)示例
上傳方案一:
先將文件上傳到七牛,再將七牛上傳返回的文件訪(fǎng)問(wèn)路徑上傳到服務(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文件,文件大小不超過(guò)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: '獲取憑證失敗,請(qǐng)重試', type: 'error' }) } }) }, //獲取音頻文件時(shí)長(zhǎng) getVideoPlayTime(file, fileList) { let self = this //獲取錄音時(shí)長(zhǎng) try { let url = URL.createObjectURL(file.raw); //經(jīng)測(cè)試,發(fā)現(xiàn)audio也可獲取視頻的時(shí)長(zhǎng) 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) } }, //校驗(yàn)上傳文件大小 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: '上傳文件不能不超過(guò)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, //播放時(shí)長(zhǎng) "size": file.size/1024, //文件大小 單位 kb "song_name": file.name, //歌曲名 "voice_url": "xxxx" //上傳七牛返回的訪(fǎng)問(wèn)路徑 }) } } 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ù)器失敗,請(qǐng)重試', type: 'error' }) } }).catch(err => { this.$message({ message: '上傳服務(wù)器失敗,請(qǐng)重試', type: 'error' }) }) }, } }
上傳方案二:
直接將文件上傳到服務(wù)器
<div class="upload-music-container"> <el-upload class="upload-music" ref="upload" multiple action="" :auto-upload="false" :http-request="uploadFile"> <el-button slot="trigger" size="small" type="primary">選取文件</el-button> <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上傳到服務(wù)器</el-button> <div slot="tip" class="el-upload__tip">只能上傳mp3文件,且單次不超過(guò)500M</div> </el-upload> </div> export default { name: 'uploadMusic', data() { return { fileType:'video', fileData: new FormData(), headers:{}, } },
補(bǔ)充:element-ui實(shí)現(xiàn)多文件加表單參數(shù)上傳
element-ui是分圖片多次上傳,一次上傳一個(gè)圖片。
如果想一次上傳多個(gè)圖片,就得關(guān)掉自動(dòng)上傳:auto-upload=‘false',同時(shí)不使用element內(nèi)置上傳函數(shù),換成自己寫(xiě)的onsubmit()
為了實(shí)現(xiàn)圖片的添加刪除,可在on-change與on-remove事件中取得filelist(filelist實(shí)質(zhì)就是uploadFiles的別名,而uploadFiles就是element內(nèi)置的用于保存待上傳文件或圖片的數(shù)組),在最后一步提交的過(guò)程中,將filelist中的值一一添加到formdata對(duì)象中(formdata.append()添加,formdata.delete()刪除),然后統(tǒng)一上傳。
ps:on-preview事件和<el-dialog>組件以及對(duì)應(yīng)屬性、方法這一體系是用來(lái)實(shí)現(xiàn)圖片的點(diǎn)擊放大功能。被注釋掉的beforeupload只有一個(gè)實(shí)參,是針對(duì)單一文件上傳時(shí)使用到的,這里無(wú)法用上
<template> <div> <el-upload action="http://127.0.0.1:8000/api/UploadFile/" list-type="picture-card" :auto-upload="false" :on-change="OnChange" :on-remove="OnRemove" :on-preview="handlePictureCardPreview" :before-remove="beforeRemove" > <i class="el-icon-plus"></i> </el-upload> <el-dialog :visible.sync="dialogVisible"> <img width="100%" :src="dialogImageUrl" alt=""> </el-dialog> <el-button type="" @click="fun">點(diǎn)擊查看filelist</el-button> <el-button type="" @click="onSubmit">提交</el-button> </div> </template> <script> import {host,batchTagInfo} from '../../api/api' export default { data() { return { param: new FormData(), form:{}, count:0, fileList:[], dialogVisible:false, dialogImageUrl:'' }; }, methods: { handlePictureCardPreview(file) { this.dialogImageUrl = file.url; this.dialogVisible = true; }, beforeRemove(file, fileList) { return this.$confirm(`確定移除 ${ file.name }?`); }, OnChange(file,fileList){ this.fileList=fileList }, OnRemove(file,fileList){ this.fileList=fileList }, //阻止upload的自己上傳,進(jìn)行再操作 // beforeupload(file) { // console.log('-------------------------') // console.log(file); // //創(chuàng)建臨時(shí)的路徑來(lái)展示圖片 // //重新寫(xiě)一個(gè)表單上傳的方法 // this.param = new FormData(); // this.param.append('file[]', file, file.name); // this.form={ // a:1, // b:2, // c:3 // } // // this.param.append('file[]', file, file.name); // this.param.append('form',form) // return true; // }, fun(){ console.log('------------------------') console.log(this.fileList) }, onSubmit(){ this.form={ a:1, b:2, c:3 } let file='' for(let x in this.form){ this.param.append(x,this.form[x]) } for(let i=0;i<this.fileList.length;i++){ file='file'+this.count this.count++ this.param.append(file,this.fileList[i].raw) } batchTagInfo(this.param) .then(res=>{ alert(res) }) } } } </script> <style> </style>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- element-ui 中使用upload多文件上傳只請(qǐng)求一次接口
- element-ui 文件上傳修改文件名的方法示例
- element-ui upload組件多文件上傳的示例代碼
- IE9 elementUI文件上傳的問(wèn)題解決
- vue+elementUI實(shí)現(xiàn)多文件上傳與預(yù)覽功能實(shí)戰(zhàn)記錄(word/PDF/圖片/docx/doc/xlxs/txt)
- Django后端分離 使用element-ui文件上傳方式
- VUE+element-ui文件上傳的示例代碼
- vue+element-ui+axios多文件上傳的實(shí)現(xiàn)并顯示整體進(jìn)度
- elementui+vue+axios實(shí)現(xiàn)文件上傳本地服務(wù)器
- VUE學(xué)習(xí)之Element-ui文件上傳實(shí)例詳解
相關(guān)文章
VUE3數(shù)據(jù)的偵聽(tīng)超詳細(xì)講解
在Vue3中watch特性進(jìn)行了一些改變和優(yōu)化,與computed不同,watch通常用于監(jiān)聽(tīng)數(shù)據(jù)的變化,并執(zhí)行一些副作用,這篇文章主要給大家介紹了關(guān)于VUE3數(shù)據(jù)偵聽(tīng)的相關(guān)資料,需要的朋友可以參考下2023-12-12element-ui循環(huán)顯示radio控件信息的方法
今天小編就為大家分享一篇element-ui循環(huán)顯示radio控件信息的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08vue實(shí)現(xiàn)tagsview多頁(yè)簽導(dǎo)航功能的示例代碼
這篇文章主要介紹了vue實(shí)現(xiàn)tagsview多頁(yè)簽導(dǎo)航功能,本文梳理了一下vue-element-admin項(xiàng)目實(shí)現(xiàn)多頁(yè)簽功能的整體步驟,需要的朋友可以參考下2022-08-08el-table?表格分頁(yè)序號(hào)問(wèn)題小結(jié)
這篇文章主要介紹了el-table?表格分頁(yè)序號(hào)問(wèn)題小結(jié),本文通過(guò)實(shí)例代碼圖文效果展示給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-04-04vue+j簡(jiǎn)單的實(shí)現(xiàn)輪播效果,滾動(dòng)公告,銜接
這篇文章主要介紹了vue+j簡(jiǎn)單的實(shí)現(xiàn)輪播效果,滾動(dòng)公告,銜接,文章圍繞主題的相關(guān)資料展開(kāi)詳細(xì)的內(nèi)容具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-06-06Vue3中進(jìn)行頁(yè)面局部刷新組件刷新的操作方法
這篇文章主要介紹了Vue3中進(jìn)行頁(yè)面局部刷新組件刷新的操作方法,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12關(guān)于vue.js中this.$emit的理解使用
本文主要介紹了關(guān)于vue.js中this.$emit的理解使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08