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

138
Vistas
Descarga de archivos MERN de blob en reaccionar

Tengo una aplicación en la que un usuario hace clic en un enlace y se le muestra una copia de una imagen de mi servidor. Mi servidor me está devolviendo el blob. Quiero darle a mi usuario la posibilidad de hacer clic en un botón en esta página y descargar el archivo desde el enlace. Este enlace puede contener un archivo jpeg o incluso un archivo csv. Intenté usar js-file-downloader pero no tuve suerte

Aquí está mi componente de reacción a continuación

 import React, {useState, useEffect} from 'react' import axios from 'axios' import { Col, Button } from 'reactstrap' import { fileDownload } from 'js-file-download'; const PreviewMedia = ({match}) => { const [file, setFile] = useState() const [image, setImage] = useState() console.log('image', image) const handleClick = () => { window.open(image) <--- this handleClick should be downloading the image state which is a blob } useEffect(() => { try { axios({ url:`http://localhost:5000/media/${match.params.imageID}`, // <----------first get request method:"GET" }).then(res => setFile(res.data)) } catch (error) { console.log(error) } }, []) useEffect(() => { console.log("this ran") if(!file) { } else { axios({ url: file?.links?.content_direct_temporary, //<----------------returns the blob second get request once done method:"GET" }).then(res => { setImage(res.data) // <--- res.data is a blob })} }, [file]) console.log("PREVIEW MEDIA-----------file", file) return ( <div> <Col className="text-center"> {image && <img src={image} ></img>} //<--successfully renders image or nothing if file which is fine </Col> <Col className="d-flex justify-content-center" > <Button color="primary" onClick={handleClick} className=" my-3 text-white"> Download file </Button> </Col> </div> ) } export default PreviewMedia
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

súper simple no hay necesidad de una biblioteca externa para esto. ESTO SOLO FUNCIONA PARA URLS DEL MISMO ORIGEN PERO SIRVE PARA MI CASO

 <div> <Col className="text-center"> {image && <img src={image} ></img>} //<--successfully renders image or nothing if file which is fine </Col> <Col className="d-flex justify-content-center" > <a href={image} download > // wrap this anchor tag around button w/ download attribute <Button color="primary" onClick={handleClick} className=" my-3 text-white"> Download file </Button> </a> </Col> </div> <a download="custom-filename.jpg" href="/path/to/image" title="ImageName"> </a>
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