怎么在Element-UI中使用Upload上傳文件前端緩存處理?針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

在做網(wǎng)站、網(wǎng)站設(shè)計(jì)中從網(wǎng)站色彩、結(jié)構(gòu)布局、欄目設(shè)置、關(guān)鍵詞群組等細(xì)微處著手,突出企業(yè)的產(chǎn)品/服務(wù)/品牌,幫助企業(yè)鎖定精準(zhǔn)用戶,提高在線咨詢和轉(zhuǎn)化,使成都網(wǎng)站營銷成為有效果、有回報的無錫營銷推廣。創(chuàng)新互聯(lián)專業(yè)成都網(wǎng)站建設(shè)十載了,客戶滿意度97.8%,歡迎成都創(chuàng)新互聯(lián)客戶聯(lián)系。
首先定義一個jsonContent, 我們的目標(biāo)是將本地選取的文件轉(zhuǎn)換為JSON賦值給jsonContent
然后我們的模板文件是利用el-dialog和el-upload兩個組件組合:這里停止文件自動上傳模式: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>
最后通過html5的filereader對變量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)
}
}為方便測試,以下是完整代碼:
<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上傳文件前端緩存處理問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。
網(wǎng)頁標(biāo)題:怎么在Element-UI中使用Upload上傳文件前端緩存處理
網(wǎng)站網(wǎng)址:http://www.chinadenli.net/article48/gspdhp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、自適應(yīng)網(wǎng)站、標(biāo)簽優(yōu)化、網(wǎng)站制作、App設(shè)計(jì)、小程序開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)