Element-UI中Upload上传文件前端缓存处理
Element-UI对于文件上传组件的功能点着重于文件传递到后台处理,所以要求action为必填属性。
但是如果需要读取本地文件并在前端直接处理,文件就没有必要传递到后台,比如在本地打开一个JSON文件,利用JSON文件在前端进行动态展示等等。
下面就展示一下具体做法:
首先定义一个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>
PS: 相关阅读
https://developer.mozilla.org...
作者:java_augur
来源:CSDN
原文:https://blog.csdn.net/java_au...
版权声明:本文为博主原创文章,转载请附上博文链接!
来源:https://segmentfault.com/a/1190000018224949
Element-UI中Upload上传文件前端缓存处理的更多相关文章
- Element UI中的上传文件功能
上传文件给后台: <el-upload style="display:inline-block" :limit=" class="upload-demo& ...
- element ui实现手动上传文件,且只能上传单个文件,并能覆盖上传。
element ui提供了成熟的组件场景,但实际工作中难免会遇到认(sha)真(diao)的产品.比如,最近遇到的,要求实现手动上传特定格式文件(用户点击“上传文件”按钮,确定之后,只是单纯选择了文件 ...
- Android应用开发中webview上传文件的几种思路
1. 常规方法,重写WebChromeClient 的 openFileChooser 方法 private class MyWebChromeClient extends WebChromeClie ...
- Struts Upload上传文件
1.Unable to find 'struts.multipart.saveDir' property setting. Defaulting to javax.servlet.context.te ...
- element-ui upload上传文件并携带参数 使用formData对象
需求:上传文件的时候,需要携带其他的参数 问题:使用upload上传文件时,必须使用formData对象,而其他的参数通过data获取的到的,formData和data是不能同时传输的 解决:获取到的 ...
- Vue+Element UI 实现视频上传
一.前言 项目中需要提供一个视频介绍,使用户能够快速.方便的了解如何使用产品以及注意事项. 前台使用Vue+Element UI中的el-upload组件实现视频上传及进度条展示,后台提供视频上传AP ...
- ASP.Net在web.config中设置上传文件的大小方法
修改Webcong文件:<system.web><httpRuntime maxRequestLength="40960" //即40MB,1KB=1024u ...
- SpringMvc (注解)中的上传文件
第一步:导入commons-fileupload-1.3.1.jar 和commons-io-2.2.jar 架包 第二步:在applicationContext.xml中 配置 <bean i ...
- 在asp.net 中怎样上传文件夹
以ASP.NET Core WebAPI 作后端 API ,用 Vue 构建前端页面,用 Axios 从前端访问后端 API ,包括文件的上传和下载. 准备文件上传的API #region 文件上传 ...
随机推荐
- ios Instruments 内存泄露
本文转载至 http://my.oschina.net/sunqichao/blog?disp=2&p=3 虽然iOS 5.0版本之后加入了ARC机制,由于相互引用关系比较复杂时,内存泄露还是 ...
- beginUpdates和endUpdates-实现UITableView的动画块
我们在做UITableView的修改,删除,选择时,需要对UITableView进行一系列的动作操作. 这样,我们就会用到 [tableView beginUpdates]; if (newCount ...
- JS Date parse
因为JS中的Date转换格式没有“-”这种间隔符,Date.parse会生成NAN,所以只能进行转换. <script type="text/javascript"> ...
- web基础----->readonly与disabled的区别
readonly和Disabled是用在表单中的两个属性,它们都能够做到使用户不能够更改表单域中的内容.但是它们之间有着微小的差别,今天我们通过案例来学习一下. readonly和Disabled的区 ...
- iOS tableview上放textfield
用UITableViewController就可以了,处理键盘弹出和消失的代码已经封装在UITableViewController里了.
- 批量远程执行linux服务器程序--基于paramiko(多线程版)
批量远程执行linux服务器程序--基于paramiko paramiko模块是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接 具体安装方法这里不写,网 ...
- onethink插件控制器如何访问?
具体路由分析就不说啦!就是那样.这里我只是方便访问来做一个记录,方便复制粘贴访问: 例如:新增一个Baoming的插件: 那么如何,访问这个控制里面方法呢? 第一种情况:这个控制器使用的是Admin模 ...
- SVN 配置和使用
SVN使用环境 使用SVN管理源代码,必须有2套环境 服务器 用来存储客户端上传的源码 一般都是在Windows环境下安装Visual SVN Server 客户端 用来提交.回退.修改.下载等操作 ...
- debug error 错误日志的调试模式
https://docs.nginx.com/nginx/admin-guide/monitoring/logging/ error_log logs/error.log warn; In this ...
- 个人理解---KMP与Next数组详解
Kmp就是求子串在母串中的位置等相关问题:当然KMP最重要的是Next数组,也称失败数组,Next[i]代表的意思是子串 sub 从sub[0] 到 sub[i-1]的前缀和后缀的最大匹配.模拟KMP ...