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="部門" 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>
如有疑問請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
淺談js多維數(shù)組和hash數(shù)組定義和使用
下面小編就為大家?guī)硪黄獪\談js多維數(shù)組和hash數(shù)組定義和使用。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-07-07
JS實(shí)現(xiàn)關(guān)鍵字搜索時(shí)的相關(guān)下拉字段效果
關(guān)鍵字搜索時(shí)有下拉字段,在使用百度時(shí)會(huì)遇到,本例講述用js實(shí)現(xiàn)類似的效果2014-08-08
JavaScript實(shí)現(xiàn)簡(jiǎn)單的日歷效果
本文給大家分享的是一個(gè)簡(jiǎn)單的JavaScript制作的日歷模板,小伙伴們可以根據(jù)自己的需求,繼續(xù)補(bǔ)充,希望大家能夠喜歡2016-09-09
javascript 中的console.log和彈出窗口alert
這篇文章主要介紹了javascript 中的console.log和彈出窗口alert 的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友參考下吧2016-08-08
JavaScript手寫數(shù)組的常用函數(shù)總結(jié)
這篇文章主要給大家介紹了關(guān)于JavaScript手寫數(shù)組常用函數(shù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
包含中國(guó)城市的javascript對(duì)象實(shí)例
這篇文章主要介紹了包含中國(guó)城市的javascript對(duì)象,以一個(gè)javascript的json數(shù)據(jù)格式存儲(chǔ)為例分析了javascript存儲(chǔ)城市信息的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08

