Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

208
Visualizações
convert weird image characters for use in image src

I get an image by GetObjectCommand in backend (nodejs,express) and then pass to frontend (react) I want to show the image with img tag but data is weird!!!

i can see it in postman : enter image description here but in console : enter image description here what should i convert it into and how ? I tried so many solutions(arraybuffer,FileReader,...) but none of them worked. Is there a problem with my backend code?

backend:

const retrieveFile = asyncHandler(async (req, res) => {
  try {
    const param = {
      Bucket: 'my-bucket',
      Key: req.params.id,
    };
    const data = await s3.send(new GetObjectCommand(param));

    data.Body.pipe(res); //---------------------> image sent to frontend
  } catch (err) {
    res.json('Error', err);
  }
});

frontend:

const [imgFromStorage, setImgFromStorage] = useState('');

const getImageFromStorage = async () => {
    try {
      const { data } = await axios.get(
        '/api/storage/retrievefile/sadcat.jpg'
      );

      setImgFromStorage(data); //------------------------> image received from backend
    } catch (err) {
      console.error(err);
    }
  };
.
.
.
  <img alt='myimg' src={imgFromStorage} /> //------------------> show image
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

FINALLY i found a way

backend:

const retrieveFile = asyncHandler(async (req, res) => {
  try {
    const param = {
      Bucket: 'my-bucket',
      Key: req.params.id,
    };
    const data = await s3.send(new GetObjectCommand(param));
    const { Body } = await s3.send(new GetObjectCommand(param));
    res.writeHead(200, {
      'Content-Type': 'image/jpeg; charset=UTF-8',
    });

    Body.pipe(res); //---------------------> image sent to frontend
  } catch (err) {
    res.json('Error', err);
  }
});

frontend:

function blobToDataURL(blob, callback) {
    var a = new FileReader();
    a.onload = function (e) {
      callback(e.target.result);
    };
    a.readAsDataURL(blob);
}

const [imgFromStorage, setImgFromStorage] = useState('');

const getImageFromStorage = async () => {
    try {
      await axios
        .get('/api/storage/retrievefile/sadcat.jpg', {
          responseType: 'blob',
        })
        .then((response) => {
          blobToDataURL(response.data, function (dataurl) {
          setImgFromStorage(dataurl); 
          });
        });
    } catch (err) {
      console.error(err);
    }
  };
.
.
.
  <img alt='myimg' src={imgFromStorage} /> //------------------> show image
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda