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

205
Views
convertir caracteres de imagen extraños para usar en image src

Obtengo una imagen de GetObjectCommand en backend (nodejs, express) y luego paso a frontend (reaccionar). ¡Quiero mostrar la imagen con la etiqueta img pero los datos son extraños!

Puedo verlo en cartero : ingrese la descripción de la imagen aquí pero en la consola : ingrese la descripción de la imagen aquí ¿En qué debo convertirlo y cómo ? Probé tantas soluciones (arraybuffer, FileReader,...) pero ninguna funcionó. ¿Hay algún problema con mi código backend?

back-end:

 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); } });

Interfaz:

 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 answers
Answer question

0

FINALMENTE encontré una manera

back-end:

 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); } });

Interfaz:

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