Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

337
Views
Cargando búfer en la imagen, reflejado como blob nulo

Tendría un servidor que está cargando una imagen en la web usando una plantilla como:

 <body><img src="data:image/jpg;base64,{{.Image}}"></body>`

Donde {{.Image}} es un búfer de datos.

Estoy tratando de recopilar este búfer a través de un cliente, solo JavaScript, usando fetch y agregarlo al elemento <img> , así que escribí lo siguiente:

 <html> <head></head> <body> <img id="photo" alt="Girl in a jacket" width="500" height="600"> </body> <script type="text/javascript"> const myRequest = new Request('http://localhost:8080/blue/', { method: 'GET', headers: new Headers(), type: "arraybuffer", mode: 'cors', cache: 'default', }); fetch(myRequest) .then(response => { var arrayBufferView = new Uint8Array(response); var blob = new Blob([arrayBufferView], {type: "image/jpeg"}); return blob}) .then(myBlob => { var urlCreator = window.URL || window.webkitURL; var imageUrl = urlCreator.createObjectURL(myBlob); // var imageUrl = URL.createObjectURL(myBlob); var img = document.querySelector("#photo"); img.src = imageUrl; }); </script> </html>

Mientras cargaba la página, obtuve que el contenido del elemento img fuera:

 <img id="photo" alt="Girl in a jacket" width="500" height="600" src="blob:null/c94ac446-55c5-4ee5-a492-5286b1935ffb">

Y no aparece nada.

ingrese la descripción de la imagen aquí

¿Cómo puedo arreglar esto?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Pude resolverlo de la siguiente manera:

 <html> <head></head> <body> <img id="photo" alt="Girl in a jacket" width="500" height="600"> </body> <script type="text/javascript"> const myRequest = new Request('http://localhost:8080/blue/', { method: 'GET', headers: new Headers(), type: "arraybuffer", mode: 'cors', cache: 'default', }); fetch(myRequest) .then(response => response.blob()) .then(blob => { var reader = new FileReader(); reader.readAsDataURL(blob); reader.onloadend = function() { var imageUrl = reader.result; var img = document.querySelector("#photo"); img.src = imageUrl; } }); </script> </html>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!