Created
October 18, 2020 18:40
-
-
Save LarryKlugerDS/90db2c8facf8e2d426b755801dc20fa8 to your computer and use it in GitHub Desktop.
File download with React.JS
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
// The React.JS code for the download method: | |
const blob = new Blob([output]); // Step 3 | |
const fileDownloadUrl = URL.createObjectURL(blob); // Step 4 | |
this.setState ({fileDownloadUrl: fileDownloadUrl}, // Step 5 | |
() => { | |
this.dofileDownload.click(); // Step 6 | |
URL.revokeObjectURL(fileDownloadUrl); // Step 7 | |
this.setState({fileDownloadUrl: ""}) | |
}) | |
// The hidden anchor element: | |
<a style={{display: "none"}} | |
download={this.fileNames[this.state.fileType]} | |
href={this.state.fileDownloadUrl} | |
ref={e=>this.dofileDownload = e} | |
>download it</a> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
wait im on a different account lol