我使用JS FileReader,並且我需要在文件讀取操作後使用此數據進行操作。 FileReader是異步讀取的,我不知道,當結果準備使用。這個怎麼做?HTML5 FileReader如何返回結果?
$(document).ready(function(){
$('#file_input').on('change', function(e){
var res = readFile(this.files[0]);
//my some manipulate with res
$('#output_field').text(res);
});
});
function readFile(file){
var reader = new FileReader(),
result = 'empty';
reader.onload = function(e)
{
result = e.target.result;
};
reader.readAsText(file);
//waiting until result is empty?
return result;
}
它顯示 「空」。
如何要等到「結果」是空的? 另一種方式?
這兩個'e's之間有區別嗎? – 2015-05-21 21:53:04
@ChrisChudzicki是的,他們是不同的。第一個'e'是[jQuery事件對象](https://api.jquery.com/category/events/event-object/)。第二個'e'是[加載事件對象](https://developer.mozilla.org/en-US/docs/Web/Events/load)。 – trafalgarx 2016-10-19 08:06:52