Created
October 8, 2019 14:15
-
-
Save marvinjude/e3866ea40a0f8f1f40057e030703b418 to your computer and use it in GitHub Desktop.
Download file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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