iview Upload組件多個(gè)文件上傳的示例代碼
使用 iview Upload 上傳組件 手動(dòng)上傳 包括單個(gè)文件和多個(gè)文件
思路:創(chuàng)建一個(gè)數(shù)組 把需要上傳的文件 push到這個(gè)數(shù)組里面
1.引用組件
2.手動(dòng)上傳,根據(jù)官方文檔 設(shè)置:before-upload ="handleUpload"等于false
(1).:before-upload 是 iview Upload 上傳組件的一個(gè)屬性 設(shè)置返回值為 false 可以阻止默認(rèn)上傳方式(自動(dòng)上傳模式)
(2).handleUpload 是方法 *備注:代碼在最后面
3.上傳方法
//創(chuàng)建 formData 對(duì)象 let formData = new FormData(); //向 formData 對(duì)象中添加文件--這是其他參數(shù) formData.append('jsid', _jsid); //多個(gè)文件上傳----------重點(diǎn)----需要吧已經(jīng)存儲(chǔ)到本地的文件加入 formData所以這里用for循環(huán) for(var i=0; i< that.file.length; i++){ formData.append("uploadFile",that.file[i]); // 文件對(duì)象 }
HTML代碼如下:
<FormItem label="應(yīng)標(biāo)資料" v-show="islook"> <template> <Upload multiple ref="upload" type="drag" :format="['docx','doc','txt', 'pdf']" :max-size="5000" :before-upload="handleUpload" :action="http"> <div style="padding: 20px 0"> <Icon type="ios-cloud-upload" size="52" style="color: #3399ff"></Icon> <p>點(diǎn)擊或者拖拽到此次上傳文件</p> </div> </Upload> <div> <ul class="file-list" v-for="(list,index) in file" :key="index"> <li>文件名: <span style="font-size:15px;">{{ list.name }}</span> <Icon type="ios-close" size="20" style="float:right;" @click="delFileList(index)"></Icon></li> </ul> </div> </template> </FormItem> <FormItem v-show="islookshenghe"> <h3>已經(jīng)提交數(shù)據(jù)-正在等待審核</h3> <Button type="primary" @click="gobackfanhui">返回</Button> </FormItem> <FormItem v-show="islook"> <Button type="primary" :loading="loading2" icon="ios-power" @click="upload"> <span v-if="!loading2">接受并提交應(yīng)標(biāo)信息</span> <span v-else>正在上傳文件中...</span> </Button> <p style="color:red;font-size:15px;" v-show="isfiletihsi">請(qǐng)上傳文件</p> </FormItem>
JS代碼
delFileList(index){ let that = this; that.file.splice(index, 1); console.log(that.file); }
handleUpload (file) { let that = this; if(that.file.length >= 5){ this.$Message.info("最多只能上傳5個(gè)文件"); }else{ that.file.push(file); } return false; }
axios提交方法代碼:
upload(){ let that = this; let _jsid = that.$route.query.id; if(that.file.length > 0){ that.loading2 = true; //創(chuàng)建 formData 對(duì)象 let formData = new FormData(); //向 formData 對(duì)象中添加文件 formData.append('jsid', _jsid); //多個(gè)文件上傳 for(var i=0; i< that.file.length; i++){ formData.append("uploadFile",that.file[i]); // 文件對(duì)象 } let config = { headers: { 'Content-Type': 'multipart/form-data' } } axios.post(that.http + "/shweb/gys/gysmsge/gysuploads.action", formData, { timeout: 10000, headers: { 'Content-Type': 'multipart/form-data' } }).then(function (rdata) { that.loading2 = false; if(rdata.data == "0"){ that.islook = false; that.islookshenghe = true; } console.log(rdata); }).catch(function (error) { that.loading2 = false; that.$Message.error('服務(wù)器錯(cuò)誤' + error); }); }else{ that.$Message.error("請(qǐng)至少上傳一個(gè)文件"); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
react+vite動(dòng)態(tài)導(dǎo)入報(bào)錯(cuò)@vite-ignore的問題及解決
這篇文章主要介紹了react+vite動(dòng)態(tài)導(dǎo)入報(bào)錯(cuò)@vite-ignore的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03vue中改變了vuex數(shù)據(jù)視圖不更新,也監(jiān)聽不到的原因及解決
這篇文章主要介紹了vue中改變了vuex數(shù)據(jù)視圖不更新,也監(jiān)聽不到的原因及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03element?select必填項(xiàng)驗(yàn)證回顯問題的解決
本文主要介紹了element?select必填項(xiàng)驗(yàn)證回顯問題的解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04深入了解vue中一鍵復(fù)制功能的實(shí)現(xiàn)
在現(xiàn)代的Web應(yīng)用中,用戶體驗(yàn)至關(guān)重要,而提供簡(jiǎn)單易用的復(fù)制功能是改善用戶體驗(yàn)的一項(xiàng)關(guān)鍵功能,本文將為大家詳細(xì)介紹Vue實(shí)現(xiàn)一鍵復(fù)制功能的具體方法,需要的可以參考下2023-11-11Element-ui/Element-plus?Vue報(bào)錯(cuò)問題及解決
這篇文章主要介紹了Element-ui/Element-plus?Vue報(bào)錯(cuò)問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01Vue 路由間跳轉(zhuǎn)和新開窗口的方式(query、params)
這篇文章主要介紹了Vue 路由間跳轉(zhuǎn)和新開窗口的方式,本文主要通過query方式和params方式介紹,需要的朋友可以參考下2019-12-12