-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.js
117 lines (109 loc) · 3.02 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import React from 'react';
import { Camera, Permissions } from 'expo';
import {Alert, Button, Platform, StyleSheet, Text, TouchableOpacity, View} from 'react-native';
export default class App extends React.Component {
state = {cameraPermissions: null, photo: null}
async componentDidMount() {
const { status } = await Permissions.askAsync(Permissions.CAMERA)
this.setState({ cameraPermissions: status })
}
takePhoto = async () => {
if (this.camera) {
const photo = await this.camera.takePictureAsync()
this.setState({ photo })
}
}
uploadImageGood = async () => {
const formData = new FormData()
formData.append('images', {
name: 'test.png',
type: 'image/png',
uri: 'https://facebook.github.io/react/logo-og.png',
})
await this.uploadImage(formData)
}
uploadImageBad = async () => {
const { photo } = this.state
const formData = new FormData()
formData.append('images', {
name: 'test.jpg',
type: 'image/jpeg',
uri: photo.uri,
})
await this.uploadImage(formData)
}
uploadImage = async (formData) => {
try {
const response = await fetch('https://www.google.com', {
body: formData,
method: 'POST'
})
Alert.alert('yes', 'worked')
console.log(response)
} catch (e) {
Alert.alert('no', 'error')
console.log(e)
}
}
render() {
const { photo, cameraPermissions } = this.state
if(!photo && cameraPermissions === 'granted') {
return (
<Camera
ref={ref => {
this.camera = ref
}}
style={{ flex: 1 }}
type='back'>
<View
style={{
backgroundColor: 'transparent',
flex: 1,
flexDirection: 'row',
}}>
<TouchableOpacity
onPress={this.takePhoto}
style={{
alignItems: 'center',
alignSelf: 'flex-end',
flex: 0.1,
}}>
<Text
style={{ color: 'white', fontSize: 18 }}>
Take Photo
</Text>
</TouchableOpacity>
</View>
</Camera>
);
}
return (
<View style={styles.container}>
<View>
<Text>This button will cause an error as it tries to upload a local file</Text>
<Button
onPress={this.uploadImageBad}
title="Upload local photo"
color="#841584"
styles={{flex:1}}
/>
<Text>This button will not cause an error as it tries to upload a file from 'https://facebook.github.io/react/logo-og.png'</Text>
<Button
onPress={this.uploadImageGood}
title="Upload remote photo"
color="#841584"
styles={{flex:1}}
/>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});