您好,登錄后才能下訂單哦!
怎么在Element-UI中使用Upload上傳文件前端緩存處理?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
首先定義一個jsonContent, 我們的目標是將本地選取的文件轉換為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中的文件進行讀取并賦值給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>
關于怎么在Element-UI中使用Upload上傳文件前端緩存處理問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。