Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Download excel using reactJS

import axios, { AxiosRequestConfig } from 'axios';
import fs from 'fs';

export const downloadXLSFile = async () => {
    
    // Its important to set the 'Content-Type': 'blob' and responseType:'arraybuffer'.
    const headers = {'Content-Type': 'blob'};
    const config: AxiosRequestConfig = {method: 'GET', url: URL, responseType: 'arraybuffer', headers};
    
    try {
        const response = await axios(config);
        
        const outputFilename = `${Date.now()}.xls`;

        // If you want to download file automatically using link attribute.
        const url = URL.createObjectURL(new Blob([response.data]));
        const link = document.createElement('a');
        link.href = url;
        link.setAttribute('download', outputFilename);
        document.body.appendChild(link);
        link.click();

        // OR you can save/write file locally.
        fs.writeFileSync(outputFilename, response.data);
    } catch (error) {
        throw Error(error);
    }
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: less than or equal to javascript 
Javascript :: Comparing and Filtering two arrays 
Javascript :: unix timestamp js 
Javascript :: logic operators in javascript 
Javascript :: add items to a react array in hooks 
Javascript :: post request with authorization 
Javascript :: react semantic button 
Javascript :: why can i put comments in some json files 
Javascript :: lifecycles if reactjs 
Javascript :: remove eslint check react native 
Javascript :: js detect if content editable div is empty 
Javascript :: validate form on submit 
Javascript :: anjular js 
Javascript :: next js styled components classname did not match 
Javascript :: redux react redux 
Javascript :: how to increment variable value in javascript 
Javascript :: javascript some 
Javascript :: express starting code 
Javascript :: js fetch 
Javascript :: mod operation in shopify 
Javascript :: check if every value in array is equal 
Javascript :: clearing setinterval 
Javascript :: learn mongodb 
Javascript :: how to remove selected characters from a string in javascript 
Javascript :: read files in node js 
Javascript :: search through json for key 
Javascript :: vim go back word 
Javascript :: discord.js ban user 
Javascript :: get filenem js 
Javascript :: load more button javascript 
ADD CONTENT
Topic
Content
Source link
Name
7+5 =