Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

button to download a file in reactJS

I am currently working on a personal portfolio, I am trying to make a button that if you click it should download a Resume.

code

<form method="get" action="fileName">
        <button class="myButton" type="submit">Download!</button>
</form>

let the user download the file.

I am working in REACTJS, create-react-app

like image 957
Daniel Isaac Chavez Avatar asked Oct 21 '25 12:10

Daniel Isaac Chavez


2 Answers

You can use FileSaver.js to achieve this goal:

Install the npm package: npm install file-saver --save

const saveFile = () => {
fileSaver.saveAs(
  process.env.REACT_APP_CLIENT_URL + "/resources/cv.pdf",
  "MyCV.pdf"
);

};

<button className="cv" onClick={saveFile}>
    Download File
</button>
like image 73
alshafi Avatar answered Oct 24 '25 02:10

alshafi


you can do it by this way

<a href="./yourfile.pdf" download>Download CV</a>
like image 44
Nemer Avatar answered Oct 24 '25 03:10

Nemer