Thursday 10 June 2021

React File Download - exportCSV Axios POST

 React File Download - exportCSV Axios POST - (blob from Spring Boot)

--------------------------------------------------------------------


component.js

---------------

import ServiceApi from './ServiceAPI';


<Button onClick={downLoadFile}> DownLoad</Button>



function downLoadFile(){


ServiceApi.exportCSV(postData).then( response =>{

const url = window.URL.createObjectURL(new Blob([response.data]));

const link = document.createElement('a');

link.href = url;

link.setAttribute('download', 'FileName.xls'); //or any other extension

document.body.appendChild(link);

link.click();

});


}



ServiceAPI.js

------------------


import axios from 'axios';


class ServiceApi {


exportCSV(postData){

return axios.post('/api/endPointURL',postData, {ContentDisposition: "attachment;filename=report.xls", responseType:

'blob'} ).catch(function(error) {

console.log(error);

});

}


export default new ServiceApi()


No comments:

Post a Comment