怎么在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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。