Created
July 9, 2018 19:31
-
-
Save dillonhafer/04936dda21f7f0331167c8ff1cc9c5eb to your computer and use it in GitHub Desktop.
Expo Document Picker workaround
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 { | |
| View, | |
| Text, | |
| TouchableOpacity, | |
| } from 'react-native'; | |
| import WebViewHack from './WebViewHack'; | |
| class ImportFileScreen extends Component { | |
| selectFile = async () => { | |
| try { | |
| const file = await DocumentPicker.getDocumentAsync({ type: 'text/csv' }); | |
| if (file.type === 'success') { | |
| this.parseFile(file.uri); | |
| } | |
| } catch (err) { | |
| // Expo didn't build with iCloud, expo turtle fallback | |
| this.webview.injectJavaScript('selectFile()'); | |
| } | |
| }; | |
| onSelectFile = event => { | |
| const base64file = event.nativeEvent.data; | |
| this.parseFile(base64file); | |
| }; | |
| parseFile(file) { | |
| // do something with the file | |
| } | |
| render() { | |
| return ( | |
| <View> | |
| <TouchableOpacity onPress={this.selectFile}> | |
| <Text>Attach File</Text> | |
| </TouchableOpacity> | |
| <WebViewHack | |
| ref={webview => { | |
| if (webview && webview.webview) { | |
| this.webview = webview.webview; | |
| } | |
| }} | |
| onSelectFile={this.onSelectFile} | |
| /> | |
| </View> | |
| ) | |
| } | |
| } |
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, { PureComponent } from 'react'; | |
| import { WebView } from 'react-native'; | |
| const html = ` | |
| <input id='csv' type="file" accept="text/csv,text/plain"> | |
| <script> | |
| function selectFile(event) { | |
| document.getElementById('csv').click(); | |
| } | |
| function getBase64(e) { | |
| var file = e.target.files[0]; | |
| var reader = new FileReader(); | |
| reader.readAsDataURL(file); | |
| reader.onload = function () { | |
| window.postMessage(reader.result) | |
| }; | |
| reader.onerror = function (error) { | |
| console.log("Something went wrong:", error) | |
| }; | |
| } | |
| document.getElementById('csv').addEventListener('change', getBase64) | |
| </script> | |
| `; | |
| class WebViewHack extends PureComponent { | |
| render() { | |
| const { onSelectFile } = this.props; | |
| return ( | |
| <WebView | |
| ref={webview => (this.webview = webview)} | |
| onMessage={onSelectFile} | |
| style={{ | |
| position: 'absolute', | |
| bottom: 0, | |
| left: 0, | |
| right: 0, | |
| width: 0, | |
| height: 0, | |
| }} | |
| source={{ | |
| html, | |
| }} | |
| /> | |
| ); | |
| } | |
| } | |
| export default WebViewHack; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this worked for me for expo