-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImagePickerExample.js
More file actions
113 lines (95 loc) · 3.07 KB
/
ImagePickerExample.js
File metadata and controls
113 lines (95 loc) · 3.07 KB
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
import * as React from "react";
import { Button, Image, View, Text, Platform } from "react-native";
import * as ImagePicker from "expo-image-picker";
import Constants from "expo-constants";
import * as Permissions from "expo-permissions";
//import RNFS from "react-native-fs";
export default class ImagePickerExample extends React.Component {
state = {
image: null,
imageContent: null,
};
render() {
let { image } = this.state;
return (
<View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}>
<Button title="Pick an image" onPress={this._pickImage} />
<Button title="Upload to server" onPress={this._uploadImage} />
{image && (
<Image source={{ uri: image }} style={{ width: 200, height: 200 }} />
)}
</View>
);
}
componentDidMount() {
this.getPermissionAsync();
}
getPermissionAsync = async () => {
if (Platform.OS !== "web") {
const { status } = await Permissions.askAsync(Permissions.CAMERA_ROLL);
if (status !== "granted") {
alert("Sorry, we need camera roll permissions to make this work!");
}
}
};
_pickImage = async () => {
try {
let result = await ImagePicker.launchImageLibraryAsync({
mediaTypes: ImagePicker.MediaTypeOptions.All,
allowsEditing: true,
aspect: [4, 3],
quality: 1,
});
if (!result.cancelled) {
this.setState({ image: result.uri });
this.setState({ imageContent: result.base64 });
}
console.log(result);
} catch (E) {
console.log(E);
}
};
_uploadImage = async () => {
let imageuri = this.state.image;
let imageContent = this.state.imageContent;
alert("Attempting upload: " + imageuri);
var https = XMLHttpRequest;
//var FormData = require("form-data");
if (imageuri != null) {
var options = {
method: "POST",
hostname: "api.myserver.xyz",
path: "/imgupload/idcard",
headers: {},
maxRedirects: 20,
};
var req = https.request(options, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function (chunk) {
var body = Buffer.concat(chunks);
console.log(body.toString());
alert("Uploaded: " + body.toString());
});
res.on("error", function (error) {
console.error(error);
});
});
var postData =
'------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name="file"; filename="test.txt"\r\nContent-Type: "image/jpeg"\r\n\r\n' +
imageContent +
"\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--";
req.setHeader(
"content-type",
"multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
);
req.write(postData);
req.end();
} else {
alert("No picture");
console.log("No picture");
}
};
}