Last active
June 25, 2018 10:04
-
-
Save memandip/b80324b376560be57dcd57b17250b444 to your computer and use it in GitHub Desktop.
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
import React, {Component} from 'react'; | |
import download from 'downloadjs'; | |
import mime from 'mime-types'; | |
class App extends Component { | |
handleClick = () => { | |
let file = '/testfile.txt'; //file url (local or remote) | |
let filename = 'testfile.txt'; | |
let mimetype = mime.lookup(filename); | |
let x = new XMLHttpRequest(); | |
x.open("GET", file, true); | |
x.responseType = 'blob'; | |
x.onload= function(e){ download(x.response, filename, mimetype ); } | |
x.send(); | |
} | |
render() { | |
return ( | |
<div className="App"> | |
<button onClick={this.handleClick}>Download file</button> | |
</div> | |
); | |
} | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment