element-ui多文件上傳的實(shí)現(xiàn)示例
上傳方案一:
先將文件上傳到七牛,再將七牛上傳返回的文件訪問(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" //上傳七牛返回的訪問(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)文章
動(dòng)畫(huà)詳解Vue3的Composition?Api
為讓大家更好的理解Vue3的Composition?Api本文采用了詳細(xì)的動(dòng)畫(huà)演繹,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
詳解使用VueJS開(kāi)發(fā)項(xiàng)目中的兼容問(wèn)題
這篇文章主要介紹了詳解使用VueJS開(kāi)發(fā)項(xiàng)目中的兼容問(wèn)題,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
VUE實(shí)現(xiàn)表單元素雙向綁定(總結(jié))
本篇文章主要介紹了VUE實(shí)現(xiàn)表單元素雙向綁定(總結(jié)) ,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08
Vue.js實(shí)現(xiàn)一個(gè)SPA登錄頁(yè)面的過(guò)程【推薦】
本篇文章主要介紹了Vue.js寫(xiě)一個(gè)SPA登錄頁(yè)面過(guò)程的相關(guān)知識(shí),具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-04-04
Vue+ECharts+高德地圖API實(shí)現(xiàn)天氣預(yù)報(bào)數(shù)據(jù)可視化的教程
所謂數(shù)據(jù)可視化,我們可以理解為從宏觀角度來(lái)看一眼就能看出來(lái)整個(gè)數(shù)據(jù)的占比,走向,對(duì)于數(shù)據(jù)可視化,很多互聯(lián)網(wǎng)公司是很看重這一塊的,包括大廠,本就將給大家介紹如何通過(guò)Vue+ECharts+高德地圖API實(shí)現(xiàn)天氣預(yù)報(bào)數(shù)據(jù)可視化2023-06-06

