elemetUi 組件--el-upload實(shí)現(xiàn)上傳Excel文件的實(shí)例
elemetUi 組件--el-upload實(shí)現(xiàn)上傳Excel文件的實(shí)例
【需求】實(shí)現(xiàn)上傳Excel文件,在上傳到服務(wù)器時(shí),還要附加一個(gè)參數(shù),在請(qǐng)求上傳文件接口前,先要進(jìn)行文件格式判斷。

【知識(shí)點(diǎn)】
1、el-upload 官方文檔中,主要用到了以下屬性:
| data | 可選參數(shù), 上傳時(shí)附帶的額外參數(shù) |
| name | 可選參數(shù), 上傳的文件字段名 |
| before-upload | 可選參數(shù), 上傳文件之前的鉤子,參數(shù)為上傳的文件,若返回 false 或者返回 Promise 且被 reject,則停止上傳。 |
2、split進(jìn)行字符串截取
【分析】
<template>
<div class="panel admin-panel">
<div class="panel-head" id="add"><strong><span class="el-icon-edit"></span><span class="title">上傳數(shù)據(jù)</span></strong></div>
<div class="body-content">
<el-form :model="ruleForm" ref="ruleForm" label-width="100px" class="form uploadform">
<el-form-item label="部門(mén)" prop="name">
<el-select v-model="form.type" placeholder="請(qǐng)選擇" style="width: 135px">
<el-option
v-for="item in options"
:key="item.value"
:label="item.label"
:value="item.value">
</el-option>
</el-select>
</el-form-item>
<el-form-item>
<el-upload
class="upload-demo"
ref="upload"
action="http://10.1.20.218:8088/gnh-webadmin-platfrom/api/v1/sendSalaryBillGeinihua"
:on-preview="handlePreview"
:before-upload="beforeAvatarUpload"
:on-remove="handleRemove"
:file-list="fileList"
:auto-upload = 'false'
:on-success = 'handleSuccess'
:data="form"
name="salaryBill">
<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">只能上傳xls/xlsx文件</div>
</el-upload>
</el-form-item>
</el-form>
</div>
</div>
</template>
<script>
export default {
data() {
return {
options: [{
value: '1',
label: '帥哥部'
}, {
value: '2',
label: '美女部'
}],
fileName:'',
fileList:[],
ruleForm: {
// name: '',
isShow: '0'
},
form:{
type:'1'
},
};
},
methods: {
submitUpload() {
this.$refs.upload.submit();
},
beforeAvatarUpload(file) {
let Xls = file.name.split('.');
if(Xls[1] === 'xls'||Xls[1] === 'xlsx'){
return file
}else {
this.$message.error('上傳文件只能是 xls/xlsx 格式!')
return false
}
},
handleRemove(file, fileList) {
},
handlePreview(file) {
},
handleSuccess(res,file,fileList){
if(res.code===20000){
this.$message({
message: '上傳成功!',
type: 'success'
});
}else {
this.$message({
message: res.msg,
type: 'error'
});
}
}
}
}
</script>
<style scope>
input[type="file"] {
display: none;
}
.el-upload-list{
width: 200px;
}
.el-select {
width: 135px;
}
</style>
如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
BootStrap fileinput.js文件上傳組件實(shí)例代碼
這篇文章主要介紹了BootStrap fileinput.js文件上傳組件實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02
詳解基于webpack2.x的vue2.x的多頁(yè)面站點(diǎn)
本篇文章主要主要介紹了基于webpack2.x的vue2.x的多頁(yè)面站點(diǎn) ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-08-08
layer的prompt彈出框,點(diǎn)擊回車(chē),觸發(fā)確定事件的方法
今天小編就為大家分享一篇layer的prompt彈出框,點(diǎn)擊回車(chē),觸發(fā)確定事件的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-09-09
詳解JS截取字符串的三個(gè)方法substring,substr,slice
js中有三個(gè)截取字符的方法,分別是substring()、substr()、slice(),平時(shí)我們可能都用到過(guò),但總是會(huì)對(duì)這些方法有點(diǎn)混淆。本文將詳細(xì)介紹一下這三者的區(qū)別,需要的可以參考一下2022-03-03
簡(jiǎn)單了解Ajax表單序列化的實(shí)現(xiàn)方法
這篇文章主要介紹了簡(jiǎn)單了解Ajax表單序列化的實(shí)現(xiàn)方法,隨著Ajax的出現(xiàn),表單序列化已經(jīng)成為一種需求,在學(xué)習(xí)原生Ajax時(shí),若用POST方法向后臺(tái)提交數(shù)據(jù)時(shí),就需要將表單序列化,需要的朋友可以參考下2019-06-06
微信小程序開(kāi)發(fā)數(shù)據(jù)緩存基礎(chǔ)知識(shí)辨析及運(yùn)用實(shí)例詳解
這篇文章主要介紹了微信小程序開(kāi)發(fā)數(shù)據(jù)緩存基礎(chǔ)知識(shí)辨析及運(yùn)用實(shí)例詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
純JS實(shí)現(xiàn)表單驗(yàn)證實(shí)例
這篇文章主要介紹了純JS實(shí)現(xiàn)表單驗(yàn)證實(shí)例,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-12-12

