国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

詳解Element-UI中上傳的文件前端處理

2024-05-06 15:36:08
字體:
供稿:網(wǎng)友

Element-UI對(duì)于文件上傳組件的功能點(diǎn)著重于文件傳遞到后臺(tái)處理,所以要求action為必填屬性。但是如果需要讀取本地文件并在前端直接處理,文件就沒有必要傳遞到后臺(tái),比如在本地打開一個(gè)JSON文件,利用JSON文件在前端進(jìn)行動(dòng)態(tài)展示等等。
下面就展示一下具體做法:

首先定義一個(gè)jsonContent, 我們的目標(biāo)是將本地選取的文件轉(zhuǎn)換為JSON賦值給jsonContent

然后我們的模板文件是利用el-dialog和el-upload兩個(gè)組件組合:這里停止文件自動(dòng)上傳模式: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對(duì)變量uploadFiles中的文件進(jìn)行讀取并賦值給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)    }   }

為方便測(cè)試,以下是完整代碼:

另外一篇文章是介紹如何將jsonContent變量中的JSON對(duì)象保存到本地文件

<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>            
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 深州市| 长武县| 根河市| 新宁县| 泰宁县| 文登市| 阿拉善右旗| 玉林市| 清徐县| 内乡县| 山东| 庄河市| 曲周县| 拉萨市| 崇阳县| 扎兰屯市| 聂荣县| 桂东县| 武乡县| 灵台县| 延安市| 江城| 密云县| 崇阳县| 乐至县| 吉水县| 东海县| 兴隆县| 朝阳市| 宜丰县| 渑池县| 沐川县| 怀柔区| 新源县| 东港市| 文昌市| 潍坊市| 华池县| 化德县| 棋牌| 阿拉善盟|