Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.8k views
in Technique[技术] by (71.8m points)

vuejs怎样导入文件并且读取文件呢?

需求是:点击上面的导入按钮。选中文件后,需要读取文件里面的全部文本内容,在下面的框里面显示出来(显示刚才读取出来的全部文本内容)。只需要纯文本即可。element ui可以实现这种效果吗?
如图:
image.png


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

解决了:
代码如下:
<input type="file" class="file"/>
<el-button type="primary" @click="readFile">导入文本</el-button>
`

readFile() {
  let fileSelect = document.querySelector('input[type=file]').files[0]//找到文件上传的元素
  let reader = new FileReader()
  if (typeof FileReader === 'undefined') {
    console.log('您的浏览器不支持FileReader接口')
    return
  }
  reader.readAsText(fileSelect, 'gb2312')//注意读取中文的是用这个编码,不是utf-8
  const _this = this
  reader.onload = function() {
    // console.log(reader.result)
    _this.$nextTick(() => {
      _this.voiceContent = reader.result
      // console.log(_this.voiceContent)
    })
  }
  console.log(reader)
}`

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...