欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-01-17 14:00:10
element-ui对于文件上传组件的功能点着重于文件传递到后台处理,所以要求action为必填属性。但是如果需要读取本地文件并在前端直接处理,文件就没有必要传递到后台,比...

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>

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