Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

338
Vistas
Loading buffer in image, reflected as blob null

I'd a server that is loading an image in the web using a template as:

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

Where {{.Image}} is a buffer data.

I'm trying to collect this buffer through a client, JavaScript only, using fetch and add it to the <img> element, so I wrote the below:

<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>

While loading the page, I got the img element content to be:

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

And nothing displayed.

enter image description here

How can I fix this?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I was able to solve it as below:

<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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda