2015-05-09 113 views
0

我正在尝试读取文件的内容(任何格式,但用内容为'hello'的hello.txt进行测试)并将二进制字符串输出到Meteor中的控制台,我是新来的框架。每当我检查控制台时,都会有一个'undefined'的新条目。我在这里错过了什么?Meteor中的filereader API如何读取二进制字符串

HTML

<div> 
Select a file: 
<input type="file" id="fileInput"> 
</div> 

JS:

if (Meteor.isClient) { 

    window.onload = function() { 
    var fileInput = document.getElementById('fileInput'); 

    fileInput.addEventListener('change', function(e) { 
     // Put the rest of the demo code here. 
     var file = fileInput.files[0]; 
     var reader = new FileReader(); 

     reader.onload = function(e) { 
     var rawData = reader.result; 
     } 

     var output = reader.readAsBinaryString(file); 
     console.log(output); 

}); 
} 
} 

回答

0

这会工作,数据将可在reader.onload方法内部,因此,这里的数据是rawData变量并移动console.log()代码到reader.onload功能

JS:

if (Meteor.isClient) { 

window.onload = function() { 
var fileInput = document.getElementById('fileInput'); 

fileInput.addEventListener('change', function(e) { 
    // Put the rest of the demo code here. 
    var file = fileInput.files[0]; 
    var reader = new FileReader(); 

    reader.onload = function(e) { 
    var rawData = reader.result; 
    console.log(rawData); 
    } 

    var output = reader.readAsBinaryString(file); 


    }); 
} 
} 
相关问题