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?

Community
  • 1
  • 1
Henry Cho
  • 730
  • 1
  • 14
  • 30
  • thanks for the question, I was starting to eat my nails because I also had the same question in my mind :) – schlingel Jan 20 '16 at 07:28

1 Answers1

1

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

It doesn't.

You missed this line from the answer of the question you linked to:

reader.readAsDataURL(input.files[0]);
Quentin
  • 800,325
  • 104
  • 1,079
  • 1,205