简体   繁体   中英

ReactJS- downloading a pdf file "Failed - no file"

I have written a React Component to download a pdf file using the achor tag of the file. I'm getting an error Failed- No file .

import React from 'react'

const Links = ()=>
        <div className='github-link'>
             <a href="https://github.com/" rel='noopener noreferrer'  alt="github link"
     target='_blank'><strong>View my GitHub here!</strong></a>
     <a href="../resume/resume.pdf" download><strong>Download my Resume!</strong></a>
        </div>



export default Links

I have my resume pdf in my src directory inside a directory called resume. The file I want to download is called resume.pdf

Thanks for any help!

Easy fix is to put the pdf file in your public folder and set href="resume.pdf" .

You can also create a folder named resume in your public and put the pdf file inside your resume folder.

<a href="/resume/resume.pdf" download><strong>Download my Resume!</strong></a>

The Trick is to Link it to the file which would be stored in the public folder.

Then

  1. Create a folder in the Public Directory of Your react app called "files"( it doesn't need to be called files in your public directory but for this example, you can do it like this ).
  2. Store the file within the folder

3) <Link to="/files/file.pdf">Download</Link>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM