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

vue實(shí)現(xiàn)文件上傳讀取及下載功能

 更新時(shí)間:2020年11月17日 15:35:34   作者:feiyu_may  
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)文件上傳讀取及下載功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了vue實(shí)現(xiàn)文件上傳讀取及下載的具體代碼,供大家參考,具體內(nèi)容如下

文件的上傳利用input標(biāo)簽的type="file"屬性,讀取用FileReader對(duì)象,下載通過(guò)創(chuàng)建a標(biāo)簽實(shí)現(xiàn)

<template>
 <div class="filediv">
 <el-button @click="downloadFile">下載</el-button>
 <div id="fileselect">
 <el-input style="margin-top: 16px" type="file"></el-input>
 </div>
 </div>
</template>
 
<script>
export default {
 data () {
 return {
 }
 },
 mounted: function () {
 this.$nextTick(function () {
 this.readFile()
 })
 },
 methods: {
 // 下載文件
 downloadFile: function () {
 var content = [
 { 'firstName': 'John', 'lastName': 'Doe' },
 { 'firstName': 'Anna', 'lastName': 'Smith' },
 { 'firstName': 'Peter', 'lastName': 'Jones' }
 ]
 var filecontent = JSON.stringify(content)
 if ('download' in document.createElement('a')) {
 this.download(filecontent, 'testfile.json')
 } else {
 alert('瀏覽器不支持')
 }
 },
 // 下載設(shè)備配置文件
 download: function (content, filename) {
 let link = document.createElement('a')
 link.download = filename
 link.style.display = 'none'
 // 字符內(nèi)容轉(zhuǎn)變成blob地址
 let blob = new Blob([content])
 link.href = URL.createObjectURL(blob)
 document.body.appendChild(link)
 link.click()
 document.body.removeChild(link)
 },
 // 導(dǎo)入設(shè)備,監(jiān)聽(tīng)上傳文件并讀取
 readFile: function () {
 console.log('讀取文件')
 let fileselect = document.querySelector('#fileselect')
 fileselect.addEventListener('change', function (e) {
 console.log(e)
 let file = e.target.files
 console.log('文件類型')
 console.log(file)
 if (file.length === 0) {
  return
 }
 let reader = new FileReader()
 if (typeof FileReader === 'undefined') {
  this.$message({
  type: 'info',
  message: '您的瀏覽器不支持FileReader接口'
  })
  return
 }
 reader.readAsText(file[0])
 reader.onload = function (e) {
  console.log('文件內(nèi)容')
  console.log(e.target.result)
 }
 }.bind(this))
 }
 }
}
</script>
 
<style scoped>
.filediv {
 width: 400px;
 margin: 20px;
}
</style>

關(guān)于vue.js的學(xué)習(xí)教程,請(qǐng)大家點(diǎn)擊專題vue.js組件學(xué)習(xí)教程、Vue.js前端組件學(xué)習(xí)教程進(jìn)行學(xué)習(xí)。

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

相關(guān)文章

最新評(píng)論