how to download file in react js

Sameer Thite picture Sameer Thite · Jun 5, 2018 · Viewed 180.3k times · Source

I receive file url as response from api. when user clicks on download button, the file should be downloaded without opening file preview in a new tab. How to achieve this in react js?

Answer

lipp picture lipp · Jun 5, 2018

This is not related to React. However, you can use the download attribute on the anchor <a> element to tell the browser to download the file.

<a href='/somefile.txt' download>Click to download</a>

This is not supported on all browsers: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a