react download file on click code example
Example 1: react js download file
fetch('https://cors-anywhere.herokuapp.com/' + fileURL, {
method: 'GET',
headers: {
'Content-Type': 'application/pdf',
},
})
.then((response) => response.blob())
.then((blob) => {
const url = window.URL.createObjectURL(
new Blob([blob]),
);
const link = document.createElement('a');
link.href = url;
link.setAttribute(
'download',
`FileName.pdf`,
);
document.body.appendChild(link);
link.click();
link.parentNode.removeChild(link);
});
Example 2: create a link in react js to download xcelfile
[{"id":1,"name":"Soumitra","email":"[email protected]"},{"id":2,"name":"Liton","email":"[email protected]"},{"id":3,"name":"Suman","email":"[email protected]"},{"id":4,"name":"Debabrata","email":"[email protected]"}]