Skip to content

Instantly share code, notes, and snippets.

@memandip
Last active June 25, 2018 10:04
Show Gist options
  • Save memandip/b80324b376560be57dcd57b17250b444 to your computer and use it in GitHub Desktop.
Save memandip/b80324b376560be57dcd57b17250b444 to your computer and use it in GitHub Desktop.
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