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上传文件前端缓存处理的更多相关文章

  1. Element UI中的上传文件功能

    上传文件给后台: <el-upload style="display:inline-block" :limit=" class="upload-demo& ...

  2. element ui实现手动上传文件,且只能上传单个文件,并能覆盖上传。

    element ui提供了成熟的组件场景,但实际工作中难免会遇到认(sha)真(diao)的产品.比如,最近遇到的,要求实现手动上传特定格式文件(用户点击“上传文件”按钮,确定之后,只是单纯选择了文件 ...

  3. Android应用开发中webview上传文件的几种思路

    1. 常规方法,重写WebChromeClient 的 openFileChooser 方法 private class MyWebChromeClient extends WebChromeClie ...

  4. Struts Upload上传文件

    1.Unable to find 'struts.multipart.saveDir' property setting. Defaulting to javax.servlet.context.te ...

  5. element-ui upload上传文件并携带参数 使用formData对象

    需求:上传文件的时候,需要携带其他的参数 问题:使用upload上传文件时,必须使用formData对象,而其他的参数通过data获取的到的,formData和data是不能同时传输的 解决:获取到的 ...

  6. Vue+Element UI 实现视频上传

    一.前言 项目中需要提供一个视频介绍,使用户能够快速.方便的了解如何使用产品以及注意事项. 前台使用Vue+Element UI中的el-upload组件实现视频上传及进度条展示,后台提供视频上传AP ...

  7. ASP.Net在web.config中设置上传文件的大小方法

    修改Webcong文件:<system.web><httpRuntime maxRequestLength="40960"   //即40MB,1KB=1024u ...

  8. SpringMvc (注解)中的上传文件

    第一步:导入commons-fileupload-1.3.1.jar 和commons-io-2.2.jar 架包 第二步:在applicationContext.xml中 配置 <bean i ...

  9. 在asp.net 中怎样上传文件夹

    以ASP.NET Core WebAPI 作后端 API ,用 Vue 构建前端页面,用 Axios 从前端访问后端 API ,包括文件的上传和下载. 准备文件上传的API #region 文件上传  ...

随机推荐

  1. ios学习--TableView详细解释

    -.建立 UITableView DataTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 420)]; [DataTa ...

  2. 图片上传根据stream生成image

    对于图片上传代码的整合 因为需要判断上传的图片的宽高是否符合尺寸,所以在最初拿到inputstream的时候,就直接获取image格式的图片 本来是想在下面的checkFile中获取的,不过直接使用S ...

  3. poj_1182 并查集

    题目大意 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B,B吃C,C吃A.现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种. 有人 ...

  4. sencha touch 入门系列 (四)sencha touch 新建项目目录结构解析

    通过上一章节的操作,我们的项目已经创建完成了, 大家通过http://127.0.0.1/MyFirst/应该都已经访问到了自己的应用, 接下来,我们展开我们项目,如图所示: 一.目录结构 1. .s ...

  5. mysql导出csv文件excel打开后数字用科学计数法显示且低位变0的解决方法

    Excel显示数字时,如果数字大于12位,它会自动转化为科学计数法:如果数字大于15位,它不仅用于科学技术费表示,还会只保留高15位,其他位都变0. Excel打开csv文件时,只要字段值都是数字,它 ...

  6. Fluent Nhibernate Mapping for Sql Views

    Views are mapped the same way tables are mapped except that you should put Readonly() in the mapping ...

  7. 【BZOJ5109】[CodePlus 2017]大吉大利,晚上吃鸡! 最短路+拓扑排序+DP

    [BZOJ5109][CodePlus 2017]大吉大利,晚上吃鸡! Description 最近<绝地求生:大逃杀>风靡全球,皮皮和毛毛也迷上了这款游戏,他们经常组队玩这款游戏.在游戏 ...

  8. iOS - 常用iOS的第三方框架

    图像:1.图片浏览控件MWPhotoBrowser       实现了一个照片浏览器类似 iOS 自带的相册应用,可显示来自手机的图片或者是网络图片,可自动从网络下载图片并进行缓存.可对图片进行缩放等 ...

  9. CentOS7安装步骤详解

    准备环境 1.虚拟机  VMware Workstation 2.Centos7-64位安装包  ( CentOS-6.7-x86_64-bin-DVD1.iso ) 开始安装   进入安装初始化界面 ...

  10. python中的接口和依赖注入

    首先,我们必须明确的一点是:python里无接口类型,定义接口只是一个人为规定,在编程过程自我约束 python的类是可以写任意个方法的 定义一个接口对继承类进行约束,接口里有什么方法,继承类就必须有 ...