UNPKG

reactjs-file-downloader

Version:

REACT File Downloader is a simple library you will be able to download file from browser and save data to file as if it was downloaded

39 lines (35 loc) 1.51 kB
import axios from "axios"; const ReactDownLoad = (url,method,filename,mime,bom) => { axios({ url: url, method: method, responseType: 'blob', withCredentials: false, headers: { 'Access-Control-Allow-Origin' : '*', 'Access-Control-Allow-Methods':'GET,PUT,POST,DELETE,PATCH,OPTIONS', }}).then( async (response) => { var blobData = (typeof bom !== 'undefined') ? [bom, response.data] : [response.data] var blob = new Blob(blobData, {type: mime || 'application/octet-stream'}); if (typeof window.navigator.msSaveBlob !== 'undefined') { window.navigator.msSaveBlob(blob, filename); } else { var blobURL = (window.URL && window.URL.createObjectURL) ? window.URL.createObjectURL(blob) : window.webkitURL.createObjectURL(blob); var tempLink = document.createElement('a'); tempLink.style.display = 'none'; tempLink.href = blobURL; tempLink.setAttribute('download', filename); if (typeof tempLink.download === 'undefined') { tempLink.setAttribute('target', '_blank'); } document.body.appendChild(tempLink); tempLink.click(); setTimeout(function() { document.body.removeChild(tempLink); window.URL.revokeObjectURL(blobURL); }, 200) } }).catch((er) => { console.log(er) }); } export default ReactDownLoad;