美文网首页
vue 上传文件

vue 上传文件

作者: 麓语 | 来源:发表于2018-07-09 11:12 被阅读0次
  • 模板部分

<input type="file" id="fileExport" @change="handleFileChange" ref="inputer">
  • js 部分

 methods:{
    handleFileChange (e) {
      let inputDOM = this.$refs.inputer;
      // 通过DOM取文件数据
      this.file    = inputDOM.files[0];
      this.errText = '';

      let size = Math.floor(this.file.size / 1024);
      console.log(this.file)
      //new一个FileReader实例
      let reader = new FileReader();
      let _this = this;
      // reader.readAsText(this.file)  //文本读取,默认utf-8,格式 其他格式:reader.readAsText(this.file, "GBK")  
      reader.readAsDataURL(this.file)  //base 64 读取
      reader.onload=function(e){
        // 读取结果
        console.log(this.result)
        // if (size > ...) {
        //     // 这里可以加个文件大小控制
        //     return false
        // }
      }
    },
}

相关文章

网友评论

      本文标题:vue 上传文件

      本文链接:https://www.haomeiwen.com/subject/xnuzuftx.html