Vue框架+Element-ui(el-upload組件)使用http-request方法上傳文件并顯示文件上傳進(jìn)度功能
頁面代碼
</el-form>
<el-form-item>
<form id="upload" enctype="multipart/form-data" method="post">
<el-upload
ref="upload"
class="upload-demo"
action="#"
:http-request="UploadImage"
:on-change="fileChange"
:before-upload="beforeUpload"
>
<!-- :before-upload="beforeUpload"-->
<el-button size="small" type="primary" class="el-icon-upload"> 數(shù)據(jù)上傳</el-button>
<!-- <div-->
<!-- slot="tip"-->
<!-- class="el-upload__tip"-->
<!-- >只能上傳db文件,且不超過一個(gè)</div>-->
</el-upload>
</form>
</el-form-item>
<!-- <el-progress v-if="progressFlag" />-->
<el-form-item label="當(dāng)前文件上傳進(jìn)度">
<el-progress style="width: 200px;margin-top: 8px" :text-inside="true" :stroke-width="20" :percentage="progressPercent" />
</el-form-item>
</el-form>script部分
引入axios import axios from 'axios'
methods代碼片段
UploadImage(param) {
if (this.filterForm.documentType === '') {
this.$message({
message: '請(qǐng)選擇檔案類型后并重新上傳',
type: 'warning'
})
return
} else if (this.filterForm.regTargetArea === '') {
this.$message({
message: '請(qǐng)選擇區(qū)縣后并重新上傳',
type: 'warning'
})
return
} else {
// 上傳新文件時(shí),將進(jìn)度條值置為零
this.progressPercent = 0
this.progressFlag = true
const formdata = new FormData()
formdata.append('documentType', this.filterForm.documentType)
formdata.append('upload', param.file)
axios({
url: 'url?token=' + this.$store.getters.token,
method: 'post',
data: formdata,
headers: { 'Content-Type': 'multipart/form-data' },
onUploadProgress: progressEvent => {
// progressEvent.loaded:已上傳文件大小
// progressEvent.total:被上傳文件的總大小
this.progressPercent = (progressEvent.loaded / progressEvent.total * 100)
console.info(progressEvent.loaded)
console.info(progressEvent.total)
}
}).then(response => {
if (response.data.rel) {
this.$message({
message: '文件上傳成功',
type: 'success'
})
// if (this.progressPercent === 100) {
// this.progressFlag = false
// this.progressPercent = 0
// }
this.logData.upUploadStatus = 1 // 是否上傳成功 1 成功 0失敗
} else {
this.logData.upUploadStatus = 0
}
this.logData.upRegion = this.filterForm.regTargetArea // areacode
this.logData.upFileUrl = response.data.filePath // 上傳文件存儲(chǔ)路徑
this.logData.upQueryType = this.filterForm.documentType // 檔案類型id
this.logData.upUploadFileSize = response.data.fileSize // 文件大小
this.logData.upUploadFileName = response.data.fileName // 文件名
// 文件上傳成功后添加日志
addUpload(this.logData).then(item => {
console.log('新增文件上傳統(tǒng)計(jì)日志成功')
}).catch(res => {
console.log('新增文件上傳統(tǒng)計(jì)日志失敗')
})
param.onSuccess() // 上傳成功的文件會(huì)顯示綠色的對(duì)勾
}).catch(response => {
this.$message({
message: '文件上傳失敗',
type: 'warning'
})
}).then(error => {
console.log(error)
})
}
}axios請(qǐng)求后臺(tái)時(shí),如有鑒權(quán)機(jī)制記得帶上token
文件上傳前做文件類型檢查
beforeUpload(file) {
console.log(file)
const testmsg = file.name.substring(file.name.lastIndexOf('.') + 1)
const extension = testmsg === 'db'
// const isLt2M = file.size / 1024 / 1024 < 10
if (!extension) {
this.$message({
message: '上傳文件只能是db格式!',
type: 'warning'
})
}
// if(!isLt2M) {
// this.$message({
// message: '上傳文件大小不能超過 10MB!',
// type: 'warning'
// });
// }
// return extension || extension2 && isLt2M
return extension
}附上效果圖(進(jìn)度條是實(shí)時(shí)動(dòng)態(tài)的)

到此這篇關(guān)于Vue框架+Element-ui(el-upload組件)使用http-request方法上傳文件并顯示文件上傳進(jìn)度的文章就介紹到這了,更多相關(guān)Vue Element-ui上傳文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Vue路由History mode模式中頁面無法渲染的原因及解決
這篇文章主要介紹了詳解Vue路由History mode模式中頁面無法渲染的原因及解決,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-09-09
vue深度監(jiān)聽(監(jiān)聽對(duì)象和數(shù)組的改變)與立即執(zhí)行監(jiān)聽實(shí)例
這篇文章主要介紹了vue深度監(jiān)聽(監(jiān)聽對(duì)象和數(shù)組的改變)與立即執(zhí)行監(jiān)聽實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09
vxe-table中vxe-grid中的合并單元格方式(合并行、列)
這篇文章主要介紹了vxe-table中vxe-grid中的合并單元格方式(合并行、列),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04
vue封裝可復(fù)用組件confirm,并綁定在vue原型上的示例
今天小編就為大家分享一篇vue封裝可復(fù)用組件confirm,并綁定在vue原型上的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-10-10
vue前端測(cè)試開發(fā)watch監(jiān)聽data的數(shù)據(jù)變化
這篇文章主要為大家介紹了vue測(cè)試開發(fā)watch監(jiān)聽data的數(shù)據(jù)變化,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
element-ui滾動(dòng)條el-scrollbar置底方式
這篇文章主要介紹了element-ui滾動(dòng)條el-scrollbar置底方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
Vue動(dòng)態(tài)設(shè)置圖片時(shí)src不生效的原因及解決方法
這篇文章主要介紹了Vue動(dòng)態(tài)設(shè)置圖片時(shí)src不生效的原因及解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
vue中實(shí)現(xiàn)methods一個(gè)方法調(diào)用另外一個(gè)方法
下面小編就為大家分享一篇vue中實(shí)現(xiàn)methods一個(gè)方法調(diào)用另外一個(gè)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-02-02

