欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Element-UI中Upload上傳文件前端緩存處理示例

 更新時(shí)間:2019年02月21日 09:39:15   作者:java_augur  
這篇文章主要介紹了Element-UI中Upload上傳文件前端緩存處理示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

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è)試,以下是完整代碼:

<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: 相關(guān)閱讀

https://developer.mozilla.org/zh-CN/docs/Web/API/FileReader

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue3實(shí)現(xiàn)CSS無限無縫滾動(dòng)效果

    vue3實(shí)現(xiàn)CSS無限無縫滾動(dòng)效果

    這篇文章主要為大家詳細(xì)介紹了vue3實(shí)現(xiàn)CSS無限無縫滾動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • vue前端實(shí)現(xiàn)驗(yàn)證碼登錄功能

    vue前端實(shí)現(xiàn)驗(yàn)證碼登錄功能

    這篇文章主要介紹了vue前端實(shí)現(xiàn)驗(yàn)證碼登錄功能,登錄時(shí)圖形驗(yàn)證通過三種方法結(jié)合實(shí)例代碼給大家講解的非常詳細(xì), 通過實(shí)例代碼介紹了vue登錄時(shí)圖形驗(yàn)證碼功能的實(shí)現(xiàn),感興趣的朋友一起看看吧
    2023-12-12
  • 基于vue中css預(yù)加載使用sass的配置方式詳解

    基于vue中css預(yù)加載使用sass的配置方式詳解

    下面小編就為大家分享一篇基于vue中css預(yù)加載使用sass的配置方式詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03
  • vue.js源代碼core scedule.js學(xué)習(xí)筆記

    vue.js源代碼core scedule.js學(xué)習(xí)筆記

    這篇文章主要為大家詳細(xì)介紹了vue.js源代碼core scedule.js的學(xué)習(xí)筆記,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • vue進(jìn)行post和get請(qǐng)求實(shí)例講解

    vue進(jìn)行post和get請(qǐng)求實(shí)例講解

    這篇文章主要介紹了vue進(jìn)行post和get請(qǐng)求實(shí)例講解,違章圍繞post和get請(qǐng)求的相關(guān)資料展開詳細(xì)內(nèi)容,具有一的的參考價(jià)值,需要的小伙伴可以參考一下
    2022-03-03
  • Vue?計(jì)算屬性之姓名案例的三種實(shí)現(xiàn)方法

    Vue?計(jì)算屬性之姓名案例的三種實(shí)現(xiàn)方法

    這篇文章主要介紹了Vue?計(jì)算屬性之姓名案例的三種實(shí)現(xiàn)方法,計(jì)算屬性實(shí)現(xiàn)、methods實(shí)現(xiàn)和插值語法實(shí)現(xiàn),下面文章具體介紹,需要的小伙伴可以參考一下
    2022-05-05
  • 詳解vue中使用vue-quill-editor富文本小結(jié)(圖片上傳)

    詳解vue中使用vue-quill-editor富文本小結(jié)(圖片上傳)

    這篇文章主要介紹了詳解vue中使用vue-quill-editor富文本小結(jié)(圖片上傳),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • LogicFlow內(nèi)置插件使用實(shí)例

    LogicFlow內(nèi)置插件使用實(shí)例

    這篇文章主要為大家介紹了LogicFlow內(nèi)置插件使用實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • vue組件間通信六種方式(總結(jié)篇)

    vue組件間通信六種方式(總結(jié)篇)

    組件是 vue.js最強(qiáng)大的功能之一,而組件實(shí)例的作用域是相互獨(dú)立的,這就意味著不同組件之間的數(shù)據(jù)無法相互引用。這篇文章主要介紹了vue組件間通信六種方式,需要的朋友可以參考下
    2019-05-05
  • vue+vuex+axio從后臺(tái)獲取數(shù)據(jù)存入vuex實(shí)現(xiàn)組件之間共享數(shù)據(jù)

    vue+vuex+axio從后臺(tái)獲取數(shù)據(jù)存入vuex實(shí)現(xiàn)組件之間共享數(shù)據(jù)

    這篇文章主要介紹了vue+vuex+axio從后臺(tái)獲取數(shù)據(jù)存入vuex,組件之間共享數(shù)據(jù),非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2017-04-04

最新評(píng)論