Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I link to local pdf file in a create-react-app project?

I have a create-react-app project in which I want to link to a pdf file I have saved in my project. I suppose my questions are twofold:

1) I'm not sure where exactly I need to save the PDF file (src folder vs public folder)

2) How do I properly link to the pdf file? Right now I have something like this:

<a href="/portfolio-revamp/public/documents/resume.pdf">Resume</a>

and it's not displaying - it changes the URL but doesn't take you to the PDF.

Any advice greatly appreciated!

like image 349
akorn3000 Avatar asked Aug 20 '18 19:08

akorn3000


2 Answers

You need to have the file in the public folder. If you want it to open in a browser, that's what it will do. If you want to force users to download, you can add the 'download' attribute to the anchor tag.

like image 117
efischency Avatar answered Oct 09 '22 10:10

efischency


  1. Place the file somewhere in src/, for example src/static/my-file.pdf.
  2. Import the file at the top of your component, like import myFile from "./static/my-file.pdf"; (path is relative to src/).
  3. Reference the object in your a tag: <a href={myFile}>Link to My File</a>
like image 35
Ryan Avatar answered Oct 09 '22 10:10

Ryan