1

Referring to this thread: Preview an image before it is uploaded

I would like to know how simply calling reader = new FileReader() lets FileReader know which file to read.

The thread link posted above works just fine for me, but when I try the following, FileReader() does not read the input. console.log returns nothing.

Javascript:

function fileReader(input){
  var reader = new FileReader();
  reader.onload = function(e){
    console.log(e.target.result);
  };
}

$(function(){
  $('#file').change(function(){
    fileReader(this);
  });
});

HTML:

<input id="file" type="file">

Why doesn't this work? Is there a way to manually give FileReader() which input DOM to read?

4

1 回答 1

1

我想知道如何简单地调用 reader = new FileReader() 让 FileReader 知道要读取哪个文件。

它没有。

您从链接到的问题的答案中错过了这一行:

reader.readAsDataURL(input.files[0]);
于 2013-07-27T12:15:12.633 回答