/
XHRExampleBinaryUpload.js
157 lines (141 loc) · 3.51 KB
/
XHRExampleBinaryUpload.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @providesModule XHRExampleBinaryUpload
*/
'use strict';
const React = require('react');
const ReactNative = require('react-native');
const {
Alert,
Linking,
Picker,
StyleSheet,
Text,
TouchableHighlight,
View,
} = ReactNative;
const BINARY_TYPES = {
String,
ArrayBuffer,
Int8Array,
Uint8Array,
Uint8ClampedArray,
Int16Array,
Uint16Array,
Int32Array,
Uint32Array,
Float32Array,
Float64Array,
DataView,
};
const SAMPLE_TEXT = `
I am the spirit that negates.
And rightly so, for all that comes to be
Deserves to perish wretchedly;
'Twere better nothing would begin.
Thus everything that that your terms, sin,
Destruction, evil represent—
That is my proper element.
--Faust, JW Goethe
`;
class XHRExampleBinaryUpload extends React.Component<{}, $FlowFixMeState> {
static handlePostTestServerUpload(xhr: XMLHttpRequest) {
if (xhr.status !== 200) {
Alert.alert(
'Upload failed',
'Expected HTTP 200 OK response, got ' + xhr.status
);
return;
}
if (!xhr.responseText) {
Alert.alert(
'Upload failed',
'No response payload.'
);
return;
}
var index = xhr.responseText.indexOf('http://www.posttestserver.com/');
if (index === -1) {
Alert.alert(
'Upload failed',
'Invalid response payload.'
);
return;
}
var url = xhr.responseText.slice(index).split('\n')[0];
console.log('Upload successful: ' + url);
Linking.openURL(url);
}
state = {
type: 'Uint8Array',
};
_upload = () => {
var xhr = new XMLHttpRequest();
xhr.open('POST', 'http://posttestserver.com/post.php');
xhr.onload = () => XHRExampleBinaryUpload.handlePostTestServerUpload(xhr);
xhr.setRequestHeader('Content-Type', 'text/plain');
if (this.state.type === 'String') {
xhr.send(SAMPLE_TEXT);
return;
}
const arrayBuffer = new ArrayBuffer(256);
const asBytes = new Uint8Array(arrayBuffer);
for (let i = 0; i < SAMPLE_TEXT.length; i++) {
asBytes[i] = SAMPLE_TEXT.charCodeAt(i);
}
if (this.state.type === 'ArrayBuffer') {
xhr.send(arrayBuffer);
return;
}
if (this.state.type === 'Uint8Array') {
xhr.send(asBytes);
return;
}
const TypedArrayClass = BINARY_TYPES[this.state.type];
xhr.send(new TypedArrayClass(arrayBuffer));
};
render() {
return (
<View>
<Text>Upload 255 bytes as...</Text>
<Picker
selectedValue={this.state.type}
onValueChange={(type) => this.setState({type})}>
{Object.keys(BINARY_TYPES).map((type) => (
<Picker.Item key={type} label={type} value={type} />
))}
</Picker>
<View style={styles.uploadButton}>
<TouchableHighlight onPress={this._upload}>
<View style={styles.uploadButtonBox}>
<Text style={styles.uploadButtonLabel}>Upload</Text>
</View>
</TouchableHighlight>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
uploadButton: {
marginTop: 16,
},
uploadButtonBox: {
flex: 1,
paddingVertical: 12,
alignItems: 'center',
backgroundColor: 'blue',
borderRadius: 4,
},
uploadButtonLabel: {
color: 'white',
fontSize: 16,
fontWeight: '500',
},
});
module.exports = XHRExampleBinaryUpload;