reader.onload函数不能读取值,也不可以调用函数

时间:2018-12-18 12:31:00

标签: javascript typescript

im使用reader.onload事件获取csv文件的内容,

问题是在console.log()中显示的值,而不是在DOM中显示的值,即通过绑定

dropped(event: UploadEvent) {
this.files = event.files;
console.log(this.files)
for (const droppedFile of event.files) {

  // Is it a file?
  if (droppedFile.fileEntry.isFile) {
    const fileEntry = droppedFile.fileEntry as FileSystemFileEntry;
    fileEntry.file((file: File) => {

      // Here you can access the real file
      console.log(droppedFile.relativePath, file);
      const fileToRead = file;
      const fileReader = new FileReader();
      fileReader.onload = this.onFileLoad;
      fileReader.readAsText(fileToRead);
      console.log(this.tempval) /// undefined 
    });
  } 
}}

和onFileLoad函数如下

onFileLoad(fileLoadedEvent) {
const textFromFileLoaded = fileLoadedEvent.target.result;         
this.csvContent = textFromFileLoaded;
var allTextLines = this.csvContent.split(/\r\n|\n/);
    var headers = allTextLines[0].split(',');
var lines = [];
    for ( var i = 0; i < allTextLines.length; i++) {
        // split content based on comma
        var data = allTextLines[i].split(',');
        if (data.length == headers.length) {
            var tarr = [];
            for ( var j = 0; j < headers.length; j++) {
                tarr.push(data[j]);
            }
            lines.push(tarr);
        }
    }this.tempval = linesconsole.log(this.tempval) // printing value 

};

不幸的是,this.tempval中的数据无法在任何地方访问 不在html DOM中或在drop()函数中。除了onFileLoad()

我刚刚是打字稿

提前感谢

0 个答案:

没有答案