Skip to content

Instantly share code, notes, and snippets.

@marvinjude
Created October 8, 2019 14:15
Show Gist options
  • Select an option

  • Save marvinjude/e3866ea40a0f8f1f40057e030703b418 to your computer and use it in GitHub Desktop.

Select an option

Save marvinjude/e3866ea40a0f8f1f40057e030703b418 to your computer and use it in GitHub Desktop.
Download file
/**
* Modern browsers can download files that aren't from same origin this is a workaround to download a remote file
* @param `url` Remote URL for the file to be downloaded
*/
function Download({ url, filename }) {
const [creatingURI, setCreatingURI] = useState(false);
const download = () => {
setCreatingURI(true);
imageDataURI
.encodeFromURL(url)
.then(uri => {
setCreatingURI(false);
const link = document.createElement("a");
link.href = uri;
link.download = `${filename}.gif`;
document.body.appendChild(link);
link.click();
})
.catch(() => {
setCreatingURI(false);
});
};
return (
<button
disabled={creatingURI}
onClick={download}
aria-label="download gif"
>
DOWNLOAD
</button>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment