Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

commented Nov 2, 2015

Hello! Thanks for sharing this.

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

@sptessier

This comment has been minimized.

Copy link

commented Nov 27, 2015

Indeed it should

@styks1987

This comment has been minimized.

Copy link

commented Feb 4, 2016

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
You can’t perform that action at this time.