简体   繁体   English

如何在javascript中使用文件阅读器将上传的文件内容显示为对象

[英]how to display the uploaded file contents as object using file reader in javascript

here i am using the file upload function to upload a json file and read it contents here the issue is the result i am getting is in string format not in object how can i display it in the object 在这里我正在使用文件上传功能上传一个json文件并读取其内容在这里,问题是我得到的结果是字符串格式而不是对象,我如何在对象中显示它

below is my code : 下面是我的代码:

.html .html

   <div class="form-group">
      <input type="file" (change)="filechanged($event)" />
   </div>

.ts .ts

filechanged(e){
       this.file = e.target.files[0];
       this.uploadDocument(this.file);
      }

 uploadDocument(file){
    let fileReader = new FileReader();
    fileReader.onload =(e) => {
      this.Name =  fileReader.result;
      console.log(typeof this.Name);
    };
    fileReader.readAsText(this.file);
  }

here i am getting the out put type as string not as object 在这里我得到的输出类型是字符串而不是对象

Parse the string using JSON.parse(<your_data>) 使用JSON.parse(<your_data>)解析字符串

  uploadDocument(file){
    let fileReader = new FileReader();
    fileReader.onload =(e) => {
      this.Name =  JSON.parse(fileReader.result);
      console.log(typeof this.Name);
    };
    fileReader.readAsText(this.file);
  }

Click here to try the code . 单击此处尝试代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM