Vue + iView實(shí)現(xiàn)Excel上傳功能的完整代碼
1、HTML部分
<Col span="2">上傳文件:</Col>
<Col span="22" class="uploadExcelBox">
<Upload ref="uploadExcel" :loading="uploadLoading" :action="uploadFileUrl" accept="xlsx,xls" :format="uploadFormat" :before-upload="beforeImgFile" :on-success="successImgFile" :on-error="errorImgFile" :show-upload-list="true">
<Button type="success">上傳附件</Button>
</Upload>
<div v-if="uploadingFile !== null">待上傳文件:
<span class="blueFont">{{ fileName }}</span>
<Button @click="uploadFun(index)" :loading="loadingStatus" class="manualUploadBtn">{{ loadingStatus ? '上傳中...' : '點(diǎn)擊開始上傳' }}</Button>
</div>
</Col>
2、JS部分
<script>
// import excel from '@/libs/excel'
import service from '@/libs/request' //用來取當(dāng)前域名
import reportFormApi from '@/api/reportForm'
export default {
data() {
return {
uploadLoading:false,//上傳控件loading狀態(tài)
uploadFileUrl: "",
uploadFormat:['xlsx','xls'],
uploadingFile:null,//待上傳的文件
loadingStatus:false,//upload組件的狀態(tài)
fileName:"",//待上傳文件的名稱
}
},
mounted() {
this.uploadFileUrl = service.apiUrl + "/qingximaster/winInfo/execlAdd";//上傳Excel的接口路徑,后端人員提供。
},
methods: {
// 圖片上傳之前
beforeImgFile(file) {
// console.log(file);
const fileExt = file.name.split('.').pop().toLocaleLowerCase()
if (fileExt === 'xlsx' || fileExt === 'xls') {
var formdata = new FormData();
formdata.append("file",file);
this.fileName = formdata.get('file').name;//通過formdata.get('file')方法,可以取file文件里的信息,例如Excel的文件名。
this.uploadingFile = formdata;//注意:這個(gè)將作為參數(shù)傳給接口實(shí)現(xiàn)上傳。傳給接口的file不需要 formdata.get('file'),直接傳file。
} else {
this.$Notice.warning({
title: '文件類型錯(cuò)誤',
desc: '文件:' + file.name + '不是EXCEL文件,請選擇后綴為.xlsx或者.xls的EXCEL文件。'
})
}
return false
},
// 上傳成功
successImgFile(response, file, fileList) {
this.$Notice.success({
title: '提示',
desc: '上傳成功!'
})
},
// 上傳失敗
errorImgFile(error, file, fileList) {
this.$Notice.success({
title: '提示',
desc: '上傳失??!'
})
console.log(error);
},
uploadFun(index){//調(diào)接口上傳Excel
this.loadingStatus = true;
reportFormApi.uploadExcel({
url: this.uploadFileUrl,
file: this.uploadingFile
}).then(res =>{
this.uploadingFile = null;
this.fileName = "";
if(res.code==0){
this.infoList[index].content = JSON.stringify(res.data);
// console.log(this.infoList[index].content);
this.$Message.success("上傳成功!");
}else{
this.$Message.error(res.message);
}
}).finally(()=>{
this.loadingStatus = false;
this.uploadLoading = false;
})
},
}
}
3、頁面效果如下
(1)進(jìn)入頁面默認(rèn)展示的樣子

(2)選中要上傳的Excel

(3)“點(diǎn)擊開始上傳”之后


以上就是Vue + iView實(shí)現(xiàn)Excel上傳的詳細(xì)內(nèi)容,更多關(guān)于vue iview excel上傳的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
前端vue+express實(shí)現(xiàn)文件的上傳下載示例
本文主要介紹了前端vue+express實(shí)現(xiàn)文件的上傳下載示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12
vue中監(jiān)聽input框獲取焦點(diǎn)及失去焦點(diǎn)的問題
這篇文章主要介紹了vue中監(jiān)聽input框獲取焦點(diǎn),失去焦點(diǎn)的問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07
vue elementUI tree樹形控件獲取父節(jié)點(diǎn)ID的實(shí)例
今天小編就為大家分享一篇vue elementUI tree樹形控件獲取父節(jié)點(diǎn)ID的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
Vue組件化(ref,props,?mixin,.插件)詳解
這篇文章主要介紹了Vue組件化(ref,?props,?mixin,?插件)的相關(guān)知識,包括ref屬性,props配置項(xiàng)及mixin混入的方式,本文通過示例代碼多種方式相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05
使vue實(shí)現(xiàn)jQuery調(diào)用的兩種方法
這篇文章主要介紹了使vue實(shí)現(xiàn)jQuery調(diào)用的兩種方法 ,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05

