element-ui?el-upload實(shí)現(xiàn)上傳文件及簡(jiǎn)單的上傳文件格式驗(yàn)證功能
在后臺(tái)管理系統(tǒng)中總是會(huì)用到上傳文件的功能,
想實(shí)現(xiàn)的樣式如下:(實(shí)現(xiàn)上傳文件后,在input輸入框顯示文件名 )

結(jié)構(gòu)代碼如下:
<el-form-item label="使用說明" class="uploadMain" prop="instruction">
<el-input
class="uploadInput"
v-model="productVO.instruction"
style="width: 75%"
placeholder="請(qǐng)上傳pdf格式的使用說明文件"
:disabled="true"
>
<el-upload
slot="append"
class="uploadbox"
ref="upload"
name="file"
accept=".pdf" //接受上傳文件的格式,此處會(huì)默認(rèn)打開上傳時(shí)篩選.pdf格式
:show-file-list="false"
:multiple="false" //如果想要一次選擇多個(gè)文件 mulitiple為true
action="upload"
:on-change="onChange"
:auto-upload="false" //自動(dòng)上傳,如果此處為true 選擇完文件就直接上傳了
>
<!-- class="uploadbtn" -->
<el-button class="uploadbtn"></el-button>
</el-upload>
</el-input>
</el-form-item>由于上述結(jié)構(gòu)代碼打開上傳文件時(shí)會(huì)自動(dòng)篩選accept的文件格式,但是在用戶選擇時(shí)仍可以自己選擇全部文件,所以需要前端對(duì)上傳文件進(jìn)行初步的格式檢驗(yàn)

前端部分上傳文件初步檢驗(yàn)js代碼如下:
onChange(file) {
// 校驗(yàn)格式
if (['application/pdf'].indexOf(file.raw.type) == -1) {
this.$message.error('請(qǐng)上傳正確的pdf格式');
return false;
}
this.productVO.instruction = file.name;
this.productVO.instructionFile = file.raw; //上傳文件時(shí)需要用到二進(jìn)制,所以這里文件取值為file.raw
},上傳至接口時(shí)js代碼如下:
submit(){
const formData = new FormData();
formData.append('instruction', this.productVO.instruction);
formData.append('instructionFile',this.productVO.instructionFile);
//調(diào)用接口
this.$post('/product/create',formData,{
baseURL:'/',
header:{isloading: true,'Content-Type': 'multipart/form-data'}).then()
}".doc" | "application/msword" | ".xls" | "application/vnd.ms-excel" |
".docx" | "application/vnd.openxmlformats-officedocument.wordprocessingml.document" | ".xlsx" | "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" |
".jpeg" | "image/jpeg" | ".mp3" | "audio/x-mpeg" |
".jpg" | "image/jpeg" | ".mp4" | "video/mp4" |
".png" | "image/png" | ".pdf" | "application/pdf" |
".ppt" | "application/vnd.ms-powerpoint" | ".txt" | "text/plain" |
".tar" | "application/x-tar" | ".wps" | "application/vnd.ms-works" |
".zip" | "application/x-zip-compressed" | ".xml" | "text/plain" |
附加:當(dāng)上傳文件為多個(gè)時(shí),具體代碼如下:
<el-form-item label="數(shù)據(jù)" class="uploadMain" prop="entity">
<el-input
class="uploadInput"
v-model="productVO.entity"
style="width: 75%"
placeholder="請(qǐng)上傳完整的tif/tiff/shp格式的數(shù)據(jù)文件"
:disabled="true"
>
<el-upload
slot="append"
class="uploadbox"
ref="upload"
name="file"
accept=".tif,.tiff,.shp,.dbf,.prj,.sbn,.sbx,.shx"
:show-file-list="false"
multiple
:file-list="this.productVO.fileList"
action="upload"
:on-change="onChange"
:auto-upload="false"
>
<!-- class="uploadbtn" -->
<el-button class="uploadbtn"></el-button>
</el-upload>
</el-input>
<div style="color: #ffc230">此處是文本說明</div>
</el-form-item>js代碼如下:
onChange(file,fileList) {
// 校驗(yàn)格式
if (['image/tiff', ''].indexOf(file4.raw.type) == -1) {
this.$message.error('請(qǐng)上傳正確的tif/tiff/shp格式');
return false;
}else{
this.productVO.fileList=fileList
console.log(this.productVO.fileList)
var listName=[]
this.productVO.fileList.forEach(function(e){listName.push(e.name)})
var listFileRaw=[]
this.productVO.fileList.forEach(function(e){listFileRaw.push(e.raw)})
this.productVO.entity = listName; //文本框顯示所有的文件名
this.productVO.entityFile = listFileRaw;
}
},接口上傳文件時(shí)formData傳參改動(dòng)如下:
this.productVO.entityFile.forEach(element => {
formData.append('entityFile', element)
})總結(jié)
到此這篇關(guān)于element-ui el-upload實(shí)現(xiàn)上傳文件及簡(jiǎn)單的上傳文件格式驗(yàn)證功能的文章就介紹到這了,更多相關(guān)element-ui el-upload上傳文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決vite.config.js無(wú)法使用__dirname的問題
這篇文章主要介紹了解決vite.config.js無(wú)法使用__dirname的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
VUE使用echarts?5.0以上版本渲染器未導(dǎo)入錯(cuò)誤問題
這篇文章主要介紹了VUE使用echarts?5.0以上版本渲染器未導(dǎo)入錯(cuò)誤問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
vue-calendar-component 封裝多日期選擇組件的實(shí)例代碼
這篇文章主要介紹了vue-calendar-component 封裝多日期選擇組件,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
vue 折疊展示多行文本組件的實(shí)現(xiàn)代碼
這篇文章主要介紹了vue 折疊展示多行文本組件,自動(dòng)根據(jù)傳入的expand判斷是否需要折疊,非常完美,文章通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-10-10
Nginx部署Vue.js前端項(xiàng)目的實(shí)現(xiàn)
本文主要介紹了Nginx部署Vue.js前端項(xiàng)目指南,幫助您實(shí)現(xiàn)從開發(fā)到線上部署的平滑過渡,確保用戶能夠獲得最佳的訪問體驗(yàn),感興趣的可以了解一下2024-09-09

