0

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?

Rrptm
  • 329
  • 2
  • 12

2 Answers2

-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
  • here i am getting error in console. Not allowed to load local resource: – Rrptm Feb 14 '21 at 18:26
  • 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
  • Any other alternative to get the desired results? – Rrptm Feb 14 '21 at 18:32
-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>