Last active
March 8, 2018 05:09
-
-
Save mars/717bbd8ef5ce82acc838 to your computer and use it in GitHub Desktop.
Use Filepicker.io with React.js (v0.14, ES2015, & JSX)
This file contains 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
/* | |
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); | |
} | |
} |
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
Indeed it should