2015-05-09 112 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); 


    }); 
} 
} 
相關問題