Question

I have a drag&drop box where users will drop an image into. I wanted to create a preview before uploading. I have the contents of the image, which contains hundreds of lines of something like this:

png contents

Is there a way to display the image with its contents? My current code for reading and assigning:

var reader = new FileReader();
reader.readAsText(uploadedfile[0]);
reader.onload = function (e) {
      filecontents = this.result;
      reader.abort();
      var image = new Image();
      image.src = filecontents;
      $('#drop p').html('<img src="'+image.src+'"/>');
}
Was it helpful?

Solution

It's very simple & efficient to do this, no FileReader required, assuming uploadedfile is an "array" of Files or Blobs:

var image = new Image();
image.src = URL.createObjectURL(uploadedfile[0]);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top