Reputation: 101
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
Upvotes: 4
Views: 30287
Reputation: 11
import MyPDF from '../path/to/file.pdf';
<a href={myPDF} download="My_File.pdf"> Download Here </a>
Replace with your stuff
Upvotes: 0
Reputation: 687
you can do it by this way
<a href="./yourfile.pdf" download>Download CV</a>
Upvotes: 4
Reputation: 1219
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>
Upvotes: 4
Reputation: 862
I have the exact same problem, and here is the solution I make use of now:
3 steps:
npm install file-saver
or something)public
folder, under a resource
or an asset
name. Webpack doesn't touch the public
folder and index.html
and your resources get copied over in production build as is, where you may refer them as shown in next step.import FileSaver from 'file-saver';
FileSaver.saveAs(
process.env.PUBLIC_URL + "/resource/file.anyType",
"fileNameYouWishCustomerToDownLoadAs.anyType");
Link
component of react-router
react-router-docs/Link. The zip file would download, and somehow would unzip properly. Generally, links have blue color, to inherit parent color scheme, simply add a prop like: style={color: inherit}
or simply assign a class of your CSS library like button button-primary
or something if you're Bootstrappin'Not useful, still good answers here:
Upvotes: 2