怎么在Element-UI中使用Upload上傳文件前端緩存處理?針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。
成都創(chuàng)新互聯(lián)制作網(wǎng)站網(wǎng)頁(yè)找三站合一網(wǎng)站制作公司,專(zhuān)注于網(wǎng)頁(yè)設(shè)計(jì),成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、外貿(mào)網(wǎng)站建設(shè),網(wǎng)站設(shè)計(jì),企業(yè)網(wǎng)站搭建,網(wǎng)站開(kāi)發(fā),建網(wǎng)站業(yè)務(wù),680元做網(wǎng)站,已為近1000家服務(wù),成都創(chuàng)新互聯(lián)網(wǎng)站建設(shè)將一如既往的為我們的客戶提供最優(yōu)質(zhì)的網(wǎng)站建設(shè)、網(wǎng)絡(luò)營(yíng)銷(xiāo)推廣服務(wù)!首先定義一個(gè)jsonContent, 我們的目標(biāo)是將本地選取的文件轉(zhuǎn)換為JSON賦值給jsonContent
然后我們的模板文件是利用el-dialog和el-upload兩個(gè)組件組合:這里停止文件自動(dòng)上傳模式:auto-upload="false"
<el-button type="primary" @click="dialogVisible = true">Load from File</el-button> <el-dialog title="Load JSON document from file" :visible.sync="dialogVisible"> <el-upload :file-list="uploadFiles" action="alert" :auto-upload="false" multiple :on-change="loadJsonFromFile"> <el-button size="small" type="primary">Select a file</el-button> <div slot="tip">upload only jpg/png files, and less than 500kb</div> </el-upload> <span slot="footer"> <el-button type="primary" @click="dialogVisible = false">cancel</el-button> <el-button type="primary" @click="loadJsonFromFileConfirmed">confirm</el-button> </span> </el-dialog>
最后通過(guò)html5的filereader對(duì)變量uploadFiles中的文件進(jìn)行讀取并賦值給jsonContent
if (this.uploadFiles) { for (let i = 0; i < this.uploadFiles.length; i++) { let file = this.uploadFiles[i] console.log(file.raw) if (!file) continue let reader = new FileReader() reader.onload = async (e) => { try { let document = JSON.parse(e.target.result) console.log(document) } catch (err) { console.log(`load JSON document from file error: ${err.message}`) this.showSnackbar(`Load JSON document from file error: ${err.message}`, 4000) } } reader.readAsText(file.raw) } }
為方便測(cè)試,以下是完整代碼:
<template> <div> <el-button type="primary" @click="dialogVisible = true">Load from File</el-button> <el-dialog title="Load JSON document from file" :visible.sync="dialogVisible"> <el-upload :file-list="uploadFiles" action="alert" :auto-upload="false" multiple :on-change="loadJsonFromFile"> <el-button size="small" type="primary">Select a file</el-button> <div slot="tip">upload only jpg/png files, and less than 500kb</div> </el-upload> <span slot="footer"> <el-button type="primary" @click="dialogVisible = false">cancel</el-button> <el-button type="primary" @click="loadJsonFromFileConfirmed">confirm</el-button> </span> </el-dialog> </div> </template> <script> export default { data () { return { // data for upload files uploadFilename: null, uploadFiles: [], dialogVisible: false } }, methods: { loadJsonFromFile (file, fileList) { this.uploadFilename = file.name this.uploadFiles = fileList }, loadJsonFromFileConfirmed () { console.log(this.uploadFiles) if (this.uploadFiles) { for (let i = 0; i < this.uploadFiles.length; i++) { let file = this.uploadFiles[i] console.log(file.raw) if (!file) continue let reader = new FileReader() reader.onload = async (e) => { try { let document = JSON.parse(e.target.result) console.log(document) } catch (err) { console.log(`load JSON document from file error: ${err.message}`) this.showSnackbar(`Load JSON document from file error: ${err.message}`, 4000) } } reader.readAsText(file.raw) } } this.dialogVisible = false } } } </script>
關(guān)于怎么在Element-UI中使用Upload上傳文件前端緩存處理問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
分享文章:怎么在Element-UI中使用Upload上傳文件前端緩存處理-創(chuàng)新互聯(lián)
標(biāo)題網(wǎng)址:http://bm7419.com/article34/dpdipe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、虛擬主機(jī)、網(wǎng)站設(shè)計(jì)、外貿(mào)網(wǎng)站建設(shè)、品牌網(wǎng)站制作、網(wǎng)站營(yíng)銷(xiāo)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容