문제

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+'"/>');
}
도움이 되었습니까?

해결책

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]);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top