element-ui多文件上傳的實現(xiàn)示例
上傳方案一:
先將文件上傳到七牛,再將七牛上傳返回的文件訪問路徑上傳到服務器
<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">上傳到服務器</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)
},
//上傳服務器數(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
},
//上傳至服務器
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: '上傳服務器成功',
type: 'success'
})
} else{
this.$message({
message: '上傳服務器失敗,請重試',
type: 'error'
})
}
}).catch(err => {
this.$message({
message: '上傳服務器失敗,請重試',
type: 'error'
})
})
},
}
}
上傳方案二:
直接將文件上傳到服務器
<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">上傳到服務器</el-button>
<div slot="tip" class="el-upload__tip">只能上傳mp3文件,且單次不超過500M</div>
</el-upload>
</div>
export default {
name: 'uploadMusic',
data() {
return {
fileType:'video',
fileData: new FormData(),
headers:{},
}
},
補充:element-ui實現(xiàn)多文件加表單參數(shù)上傳
element-ui是分圖片多次上傳,一次上傳一個圖片。
如果想一次上傳多個圖片,就得關(guān)掉自動上傳:auto-upload=‘false',同時不使用element內(nèi)置上傳函數(shù),換成自己寫的onsubmit()
為了實現(xiàn)圖片的添加刪除,可在on-change與on-remove事件中取得filelist(filelist實質(zhì)就是uploadFiles的別名,而uploadFiles就是element內(nèi)置的用于保存待上傳文件或圖片的數(shù)組),在最后一步提交的過程中,將filelist中的值一一添加到formdata對象中(formdata.append()添加,formdata.delete()刪除),然后統(tǒng)一上傳。
ps:on-preview事件和<el-dialog>組件以及對應屬性、方法這一體系是用來實現(xiàn)圖片的點擊放大功能。被注釋掉的beforeupload只有一個實參,是針對單一文件上傳時使用到的,這里無法用上
<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">點擊查看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的自己上傳,進行再操作
// beforeupload(file) {
// console.log('-------------------------')
// console.log(file);
// //創(chuàng)建臨時的路徑來展示圖片
// //重新寫一個表單上傳的方法
// 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>
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- element-ui 中使用upload多文件上傳只請求一次接口
- element-ui 文件上傳修改文件名的方法示例
- element-ui upload組件多文件上傳的示例代碼
- IE9 elementUI文件上傳的問題解決
- vue+elementUI實現(xiàn)多文件上傳與預覽功能實戰(zhàn)記錄(word/PDF/圖片/docx/doc/xlxs/txt)
- Django后端分離 使用element-ui文件上傳方式
- VUE+element-ui文件上傳的示例代碼
- vue+element-ui+axios多文件上傳的實現(xiàn)并顯示整體進度
- elementui+vue+axios實現(xiàn)文件上傳本地服務器
- VUE學習之Element-ui文件上傳實例詳解
相關(guān)文章
element-ui循環(huán)顯示radio控件信息的方法
今天小編就為大家分享一篇element-ui循環(huán)顯示radio控件信息的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08
vue實現(xiàn)tagsview多頁簽導航功能的示例代碼
這篇文章主要介紹了vue實現(xiàn)tagsview多頁簽導航功能,本文梳理了一下vue-element-admin項目實現(xiàn)多頁簽功能的整體步驟,需要的朋友可以參考下2022-08-08
關(guān)于vue.js中this.$emit的理解使用
本文主要介紹了關(guān)于vue.js中this.$emit的理解使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-08-08

