How to download a local file say demo.txt stored in d drive. From the server call, I am getting the local file location. How to download it on the onClick event?
Asked
Active
Viewed 456 times
0
-
you can check this answer https://stackoverflow.com/questions/50694881/how-to-download-file-in-react-js – Belhadjer Samir Feb 14 '21 at 17:55
-
is your file in the client machine or in your server? – Belhadjer Samir Feb 14 '21 at 18:41
2 Answers
-1
Triggering browser download from front-end is not reliable. What you should do is, create an endpoint that when called, will provide the correct response headers, thus triggering the browser download. check this answer
This is not related to React, you can use the download attribute on the anchor .
<a href='/somefile.txt' download>Click to download</a>
note
The 'download' attribute , might just open the file in a new tab depending on the browser.

Belhadjer Samir
- 1,461
- 7
- 15
-
-
this is a Security exception built into Chrome and other modern browsers. The wording may be different but in some way shape or form they all have security exceptions in place to deal with this scenario. – Belhadjer Samir Feb 14 '21 at 18:30
-
-1
just create an anchor link with href attribute relative path to the file you want to download like this:
<a href="/someFolder/filename.ext" target='_blank'>download</a>

shahrooz bazrafshan
- 545
- 4
- 17