2014-12-12 2 views

답변

3

을 FileReader

function showData() { 
 
    var file = document.getElementById('myFile'); 
 
    var data = file.files[0]; 
 
    var fileRead = new FileReader(); 
 

 
    fileRead.readAsDataURL(data); 
 

 

 
    document.getElementById('out').appendChild(document.createTextNode(' ' + fileRead.result)); 
 
}
<p id = "out">The content of the file is</p> 
 
<input type = "file" id = "myFile" style="margin-top:5%;"> 
 
<button id = "show" onclick="showData()">The result is</button>
그러므로 당신이 데이터를 읽은 후에 호출되는 콜백을 설정해야 비동기식입니다. 여기에 readAsDataURL이 적합하지 않으면 readAsText이 필요합니다.

fileRead.onload = function() { 
     document.getElementById('out').appendChild(document.createTextNode(' ' + fileRead.result)); 
} 

fileRead.readAsText(data); 
관련 문제