详解Element-UI中上传的文件前端处理

所属分类: 网络编程 / JavaScript 阅读数: 1445
收藏 0 赞 0 分享

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)
    }
   }

为方便测试,以下是完整代码:

另外一篇文章是介绍如何将jsonContent变量中的JSON对象保存到本地文件

<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>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

jQuery-1.9.1源码分析系列(十)事件系统之事件包装

这篇文章主要介绍了jQuery-1.9.1源码分析系列(十)事件系统之事件包装的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

javascript实现Email邮件显示与删除功能

这篇文章主要介绍了javascript实现Email邮件显示与删除功能,需要的朋友可以参考下
收藏 0 赞 0 分享

JavaScript实现自动生成网页元素功能(按钮、文本等)

这篇文章主要介绍了JavaScript实现自动生成网页元素功能,文章列出了三种可以进行增删改克隆按钮、文本等元素的方法,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

js实现索引图片切换效果

这篇文章主要介绍了js实现索引图片切换效果的代码,特别炫酷的效果,推荐给大家,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

javascript实现动态统计图开发实例

这篇文章主要介绍了javascript实现动态统计图开发实例,需要的朋友可以参考下
收藏 0 赞 0 分享

超详细的javascript数组方法汇总

这篇文章主要对javascript的数组方法进行了详细的汇总,包括了最常用的的数组方法,还有扩展方法,很全面,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

jQuery实现分隔条左右拖动功能

这篇文章主要介绍了jQuery实现分隔条左右拖动功能,需要的朋友可以参考下
收藏 0 赞 0 分享

分享经典的JavaScript开发技巧

这篇文章向大家分享了40多个经典的JavaScript开发技巧,相信读完这篇文章对大家开发javascript有很大的帮助作用,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

jquery实现select选择框内容左右移动代码分享

这篇文章主要介绍了jquery实现select选择框内容左右移动代码,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

解决jquery插件:TypeError:$.browser is undefined报错的方法

这篇文章为大家分享了一个解决jquery插件:TypeError:$.browser is undefined报错的方法,解决报错问题的方法也很简单,需要解决此类问题的朋友不要错过这篇文章。
收藏 0 赞 0 分享
查看更多