Skip to content

Instantly share code, notes, and snippets.

@mars
Last active March 8, 2018 05:09
Show Gist options
  • Save mars/717bbd8ef5ce82acc838 to your computer and use it in GitHub Desktop.
Save mars/717bbd8ef5ce82acc838 to your computer and use it in GitHub Desktop.
Use Filepicker.io with React.js (v0.14, ES2015, & JSX)
/*
Filepicker lib must be loaded from script tag in HTML:
<script type="text/javascript" src="//api.filepicker.io/v2/filepicker.js"></script>
*/
class FilepickerInput extends React.Component {
componentDidMount() {
const filepickerElement = this.refs.filepicker;
if (typeof filepicker !== 'undefined') {
// Single-page app integration: https://developers.filepicker.com/docs/support/integration/117
filepicker.constructWidget(filepickerElement);
}
filepickerElement.addEventListener('change', this.handleChangeFileUrl, false);
}
componentWillUnmount() {
this.refs.filepicker.removeEventListener('change', this.handleChangeFileUrl, false);
}
render() {
return <input
data-fp-apikey={this.props.filepickerApiKey}
ref="filepicker"
type="filepicker"/>
}
handleChangeFileUrl(e) {
console.log('Filepicker URL', e.target.value);
}
}
@spencer-brown
Copy link

Hello! Thanks for sharing this.

Should line 13 be filepickerElement.addEventListener('change',this.handleChangeFileUrl, false); ?

@sptessier
Copy link

Indeed it should

@styks1987
Copy link

You can also use
var filepicker = require('filepicker-js');
for something like webpack

Also, the event passed back has lots of good information.
ex.

    fpfile: Object
        client: "computer"
        filename: "img.jpg"
        id: 1
        isWriteable: true
        mimetype: "image/jpeg"
        size: 31322
        url: "https://cdn.filepicker.io/api/file/RjVvmPk7RW6A4vkNfiRQ"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment