0

I did not found answer. The same problem is How to convert file to base64 in JavaScript?.

previewPhoto (photo, id) {
  const preview = document.getElementById(id)
  const reader = new FileReader()

  reader.onloadend = function () {
    preview.src = reader.result
  }
  reader.readAsDataURL(photo)
},

There I just replace the src value in element by querySelector. But in may case I need something like this:

previewPhoto (photo) {
  const readyToSrcImg = fileToImg(photo)

  return readyToSrcImg
},

Does anyone know about this case?

1 Answers1

1

Don't use the FileReader to preview a image as base64 - it waste time to encode/decode and uses more memory - meaning: it's slower.

Use URL.createObjectURL(file) to create a references link to the raw binary instead

previewPhoto (photo, id) {
  const preview = document.getElementById(id)
  preview.src = URL.createObjectURL(photo)
},
Endless
  • 34,080
  • 13
  • 108
  • 131