@geneui/components
Version:
The Gene UI components library designed for BI tools
19 lines (16 loc) • 582 B
JavaScript
import { useCallback } from 'react';
const useImgDownload = () => useCallback(function (url, name) {
let customHeaders = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
fetch(url, {
headers: {
...customHeaders
}
}).then(response => response.blob()).then(blob => {
const blobURL = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = blobURL;
a.download = name && name.length ? name : 'download';
a.click();
}).catch(error => console.log(error));
}, []);
export { useImgDownload as default };