elementUI中el-upload文件上傳的實現(xiàn)方法
使用elementUI中的el-upload組件來實現(xiàn)文件上傳
http-request:允許使用自定義的方法來處理文件上傳的請求。
before-upload:是Element UI的<el-upload>組件的一個鉤子函數(shù),它在上傳文件之前被調(diào)用。你可以在這個鉤子中進(jìn)行一些自定義的校驗或者操作。例如設(shè)置上傳文件大小
on-change:on-change 事件在文件狀態(tài)改變時觸發(fā)。這里的文件狀態(tài)通常指上傳進(jìn)度的變化或者上傳操作的成功/失敗。on-change 事件的處理函數(shù)會接收一個參數(shù),該參數(shù)是一個對象,包含了文件的相關(guān)信息,如 file(文件對象),fileList(文件列表),name(事件名稱)等。
<span>
<el-form-item label="文件地址:" prop=""/>
</span>
<el-upload
ref="upload"
:file-list="formFileUploadForm"
:http-request="handleUploadForm"
:on-change="handleChange"
:before-upload="beforeUpload"
:show-file-list="false"
:auto-upload="false"
class="upload-demo"
action=""
>
<el-input v-model = 'uploadForm.file' type="primary" readonly styele="width:240px;line-height:0px;padding-right:20px">
<el-button size="mediumTwo" type="primary">預(yù)覽</el-button>
</el-upload>handleUploadForm(params){
const formData = new FormData()
formData.append('file',param.file)
form.append('FileName','')
axios({
url:'',
methods:'post',
headers:{'Content-Type':'multipart/form-data'},
data:formData
}).then(res=>{
if(res.data.code==200){
return this.$pop(res.data.data,'success',this)
}else{
return this.$pop(res.data.message,'warning',this)
}
}).catch(err=>{
return this.$pop(error,'error',this)
}
})
}beforeUpload(file) {
console.log('文件上傳前的校驗', file);
// 這里可以進(jìn)行一些文件的校驗,比如文件類型,文件大小等
// 返回 false 或者返回一個 Promise 對象,Promise 對象 reject 時不會上傳文件
return new Promise((resolve, reject) => {
if (file.size / 1024 / 1024 > 2) {
this.$message.error('文件大小不能超過 2MB!');
reject(new Error('文件大小不能超過 2MB!'));
} else {
resolve(file);
}
});
}, handleChange(response, file, fileList) {
if (response.status === 'success') {
console.log('文件上傳成功', file);
} else if (response.status === 'error') {
console.log('文件上傳失敗', file);
}
}總結(jié)
到此這篇關(guān)于elementUI中el-upload文件上傳實現(xiàn)的文章就介紹到這了,更多相關(guān)el-upload文件上傳內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue2.0 element-ui中el-select選擇器無法顯示選中的內(nèi)容(解決方法)
這篇文章主要介紹了vue2.0 element-ui中的el-select選擇器無法顯示選中的內(nèi)容,在文中小編使用的是element-ui V2.2.3。具體解決方法及示例代碼大家參考下本文2018-08-08
Vue電商網(wǎng)站首頁內(nèi)容吸頂功能實現(xiàn)過程
電商網(wǎng)站的首頁內(nèi)容會比較多,頁面比較長,為了能讓用戶在滾動瀏覽內(nèi)容的過程中都能夠快速的切換到其它分類。需要分類導(dǎo)航一直可見,所以需要一個吸頂導(dǎo)航的效果。目標(biāo):完成頭部組件吸頂效果的實現(xiàn)2023-04-04
vue.js聲明式渲染和條件與循環(huán)基礎(chǔ)知識
這篇文章主要為大家詳細(xì)介紹了vue.js聲明式渲染和條件與循環(huán)的基礎(chǔ)知識,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07

