Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download file by clicking a button in ReactJS

Tags:

html

reactjs

I have a website built with ReactJS and there are some files (PDF,DOC etc) that I want to let visitors download.

But simple href tag doesn't start the download, instead refresh the page.

<Button
          href="src/assets/files/exampleDoc.pdf"
          color="transparent"
          target="_blank"
          className={classes.navLink}
>
  1. Why the href tag doesn't work in reactJS to download files?
  2. How to download files with ReactJs using buttons?
like image 340
Salitha Indrajith Pathiraja Avatar asked Sep 10 '25 18:09

Salitha Indrajith Pathiraja


1 Answers

The following approach worked for me.

import ExampleDoc from '......src/assets/files/exampleDoc.pdf'

<a href={ExampleDoc} download="MyExampleDoc" target='_blank'>
   <Button className={classes.navLink}>My Example Doc</Button>
</a>
like image 142
Sai Lakshmy Avatar answered Sep 12 '25 09:09

Sai Lakshmy