2

I am trying to download file from absolute url in react js.

Any help or suggestions are appreciated.

Akhil B
  • 69
  • 3
  • 10

3 Answers3

1

If you're using React Router, use the below given code.

<Link to="/document/mydocument.pdf" target="_blank" download>Download</Link>

Where /document/mydocument.pdf is inside your public folder.

Kundan
  • 1,754
  • 16
  • 37
  • 1
    This works if i give relative url, but in my case im using absolute url like https://images.pexels.com/photos/414612/pexels-photo-414612.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500 – Akhil B Jan 13 '20 at 07:18
0

Just do it like this with download attribute.

<a href="path to file" download> Download </a>
Pushprajsinh Chudasama
  • 7,772
  • 4
  • 20
  • 43
0

It would be best if you could create an endpoint which will give you the file with the correct headers. When you create the endpoint, look for Content-Disposition and Content-Type. This will trigger an automatic and consistent download through-out all browsers.

The problem is that front-end code regarding download and the download attribute can behave differently depending on the browser. If you are not able to serve them through an endpoint, I think you will always need to keep the trade-offs in mind like inconsistent behavior through different browsers.

NewVigilante
  • 1,291
  • 1
  • 7
  • 23