Created
November 3, 2016 08:49
-
-
Save raminious/214a3cf9edb19523eda047c857004128 to your computer and use it in GitHub Desktop.
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
/** | |
* Sample React Native App | |
* https://github.com/facebook/react-native | |
* @flow | |
*/ | |
import React, { Component } from 'react'; | |
import { | |
AppRegistry, | |
StyleSheet, | |
Text, | |
View, | |
TextInput, | |
TouchableOpacity | |
} from 'react-native' | |
import RNFetchBlob from 'react-native-fetch-blob' | |
export default class blob extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
url: '', | |
path: null | |
} | |
} | |
download = () => { | |
const {url} = this.state | |
const filename = 'video.mp4' | |
const path = RNFetchBlob.fs.dirs.DocumentDir + '/' + filename | |
console.log(path) | |
console.log(' [ + ] Start Downloading') | |
RNFetchBlob | |
.config({ | |
fileCache: true, | |
path | |
}) | |
.fetch('GET', url) | |
.then((res) => { | |
this.setState({ path: res.path() }) | |
console.log('[ + ] File is downloaded on ', res) | |
}, e => { | |
console.log(e) | |
}) | |
} | |
openfile = () => { | |
RNFetchBlob.ios.openDocument(this.state.path).then(r => r, e => e) | |
} | |
render() { | |
return ( | |
<View style={styles.container}> | |
<TextInput | |
value={this.state.url} | |
onChangeText={(url) => this.setState({url})} | |
placeholder='enter url' | |
style={{ width: 300, height: 30 }} /> | |
<TouchableOpacity onPress={this.download}> | |
<Text>Download</Text> | |
</TouchableOpacity> | |
{ | |
this.state.path != null && | |
<View style={{ marginTop: 40 }}> | |
<TouchableOpacity onPress={this.openfile}> | |
<Text>Open { this.state.path }</Text> | |
</TouchableOpacity> | |
</View> | |
} | |
</View> | |
); | |
} | |
} | |
const styles = StyleSheet.create({ | |
container: { | |
flex: 1, | |
marginTop: 100, | |
backgroundColor: '#F5FCFF', | |
} | |
}); | |
AppRegistry.registerComponent('blob', () => blob); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment