2017-01-15 02:55:42 +00:00
|
|
|
/**
|
2018-09-11 22:27:47 +00:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2017-01-15 02:55:42 +00:00
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2017-01-15 02:55:42 +00:00
|
|
|
*
|
2018-05-11 20:32:37 +00:00
|
|
|
* @format
|
2017-01-15 02:55:42 +00:00
|
|
|
* @flow
|
|
|
|
*/
|
2018-05-11 20:32:37 +00:00
|
|
|
|
2017-01-15 02:55:42 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const React = require('react');
|
|
|
|
const ReactNative = require('react-native');
|
|
|
|
const {
|
|
|
|
Alert,
|
|
|
|
Platform,
|
|
|
|
ProgressBarAndroid,
|
|
|
|
ProgressViewIOS,
|
|
|
|
StyleSheet,
|
|
|
|
Switch,
|
|
|
|
Text,
|
|
|
|
TouchableHighlight,
|
|
|
|
View,
|
|
|
|
} = ReactNative;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert number of bytes to MB and round to the nearest 0.1 MB.
|
|
|
|
*/
|
|
|
|
function roundKilo(value: number): number {
|
|
|
|
return Math.round(value / 1000);
|
|
|
|
}
|
|
|
|
|
2017-08-18 01:36:54 +00:00
|
|
|
class ProgressBar extends React.Component<$FlowFixMeProps> {
|
2017-01-15 02:55:42 +00:00
|
|
|
render() {
|
|
|
|
if (Platform.OS === 'android') {
|
|
|
|
return (
|
|
|
|
<ProgressBarAndroid
|
|
|
|
progress={this.props.progress}
|
|
|
|
styleAttr="Horizontal"
|
|
|
|
indeterminate={false}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2018-05-11 20:32:37 +00:00
|
|
|
return <ProgressViewIOS progress={this.props.progress} />;
|
2017-01-15 02:55:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-18 01:36:54 +00:00
|
|
|
class XHRExampleDownload extends React.Component<{}, Object> {
|
2017-01-15 02:55:42 +00:00
|
|
|
state: Object = {
|
|
|
|
downloading: false,
|
|
|
|
// set by onreadystatechange
|
|
|
|
contentLength: 1,
|
|
|
|
responseLength: 0,
|
|
|
|
// set by onprogress
|
|
|
|
progressTotal: 1,
|
|
|
|
progressLoaded: 0,
|
|
|
|
|
|
|
|
readystateHandler: false,
|
|
|
|
progressHandler: true,
|
|
|
|
arraybuffer: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
xhr: ?XMLHttpRequest = null;
|
|
|
|
cancelled: boolean = false;
|
|
|
|
|
|
|
|
_download = () => {
|
|
|
|
let xhr;
|
|
|
|
if (this.xhr) {
|
|
|
|
xhr = this.xhr;
|
|
|
|
xhr.abort();
|
|
|
|
} else {
|
|
|
|
xhr = this.xhr = new XMLHttpRequest();
|
|
|
|
}
|
|
|
|
|
|
|
|
const onreadystatechange = () => {
|
|
|
|
if (xhr.readyState === xhr.HEADERS_RECEIVED) {
|
2018-05-11 20:32:37 +00:00
|
|
|
const contentLength = parseInt(
|
|
|
|
xhr.getResponseHeader('Content-Length'),
|
|
|
|
10,
|
|
|
|
);
|
2017-01-15 02:55:42 +00:00
|
|
|
this.setState({
|
|
|
|
contentLength,
|
|
|
|
responseLength: 0,
|
|
|
|
});
|
|
|
|
} else if (xhr.readyState === xhr.LOADING && xhr.response) {
|
|
|
|
this.setState({
|
|
|
|
responseLength: xhr.response.length,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2018-05-11 20:32:37 +00:00
|
|
|
const onprogress = event => {
|
2017-01-15 02:55:42 +00:00
|
|
|
this.setState({
|
|
|
|
progressTotal: event.total,
|
|
|
|
progressLoaded: event.loaded,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
if (this.state.readystateHandler) {
|
|
|
|
xhr.onreadystatechange = onreadystatechange;
|
|
|
|
}
|
|
|
|
if (this.state.progressHandler) {
|
|
|
|
xhr.onprogress = onprogress;
|
|
|
|
}
|
|
|
|
if (this.state.arraybuffer) {
|
|
|
|
xhr.responseType = 'arraybuffer';
|
|
|
|
}
|
|
|
|
xhr.onload = () => {
|
|
|
|
this.setState({downloading: false});
|
|
|
|
if (this.cancelled) {
|
|
|
|
this.cancelled = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (xhr.status === 200) {
|
2018-05-11 20:32:37 +00:00
|
|
|
let responseType = `Response is a string, ${
|
|
|
|
xhr.response.length
|
|
|
|
} characters long.`;
|
2017-01-15 02:55:42 +00:00
|
|
|
if (xhr.response instanceof ArrayBuffer) {
|
2018-05-11 20:32:37 +00:00
|
|
|
responseType = `Response is an ArrayBuffer, ${
|
|
|
|
xhr.response.byteLength
|
|
|
|
} bytes long.`;
|
2017-01-15 02:55:42 +00:00
|
|
|
}
|
|
|
|
Alert.alert('Download complete!', responseType);
|
|
|
|
} else if (xhr.status !== 0) {
|
|
|
|
Alert.alert(
|
|
|
|
'Error',
|
2018-05-11 20:32:37 +00:00
|
|
|
`Server returned HTTP status of ${xhr.status}: ${xhr.responseText}`,
|
2017-01-15 02:55:42 +00:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
Alert.alert('Error', xhr.responseText);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
xhr.open('GET', 'http://aleph.gutenberg.org/cache/epub/100/pg100.txt.utf8');
|
|
|
|
// Avoid gzip so we can actually show progress
|
|
|
|
xhr.setRequestHeader('Accept-Encoding', '');
|
|
|
|
xhr.send();
|
|
|
|
|
|
|
|
this.setState({downloading: true});
|
2018-05-11 20:32:37 +00:00
|
|
|
};
|
2017-01-15 02:55:42 +00:00
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.cancelled = true;
|
|
|
|
this.xhr && this.xhr.abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const button = this.state.downloading ? (
|
|
|
|
<View style={styles.wrapper}>
|
|
|
|
<View style={styles.button}>
|
|
|
|
<Text>Downloading...</Text>
|
|
|
|
</View>
|
|
|
|
</View>
|
|
|
|
) : (
|
2018-05-11 20:32:37 +00:00
|
|
|
<TouchableHighlight style={styles.wrapper} onPress={this._download}>
|
2017-01-15 02:55:42 +00:00
|
|
|
<View style={styles.button}>
|
|
|
|
<Text>Download 5MB Text File</Text>
|
|
|
|
</View>
|
|
|
|
</TouchableHighlight>
|
|
|
|
);
|
|
|
|
|
|
|
|
let readystate = null;
|
|
|
|
let progress = null;
|
|
|
|
if (this.state.readystateHandler && !this.state.arraybuffer) {
|
2018-05-11 20:32:37 +00:00
|
|
|
const {responseLength, contentLength} = this.state;
|
2017-01-15 02:55:42 +00:00
|
|
|
readystate = (
|
|
|
|
<View>
|
|
|
|
<Text style={styles.progressBarLabel}>
|
2018-05-11 20:32:37 +00:00
|
|
|
responseText: {roundKilo(responseLength)}/{roundKilo(contentLength)}k
|
|
|
|
chars
|
2017-01-15 02:55:42 +00:00
|
|
|
</Text>
|
2018-05-11 20:32:37 +00:00
|
|
|
<ProgressBar progress={responseLength / contentLength} />
|
2017-01-15 02:55:42 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (this.state.progressHandler) {
|
2018-05-11 20:32:37 +00:00
|
|
|
const {progressLoaded, progressTotal} = this.state;
|
2017-01-15 02:55:42 +00:00
|
|
|
progress = (
|
|
|
|
<View>
|
|
|
|
<Text style={styles.progressBarLabel}>
|
2018-05-11 20:32:37 +00:00
|
|
|
onprogress: {roundKilo(progressLoaded)}/{roundKilo(progressTotal)}{' '}
|
|
|
|
KB
|
2017-01-15 02:55:42 +00:00
|
|
|
</Text>
|
2018-05-11 20:32:37 +00:00
|
|
|
<ProgressBar progress={progressLoaded / progressTotal} />
|
2017-01-15 02:55:42 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<View>
|
|
|
|
<View style={styles.configRow}>
|
|
|
|
<Text>onreadystatechange handler</Text>
|
|
|
|
<Switch
|
|
|
|
value={this.state.readystateHandler}
|
2018-05-11 20:32:37 +00:00
|
|
|
onValueChange={readystateHandler =>
|
|
|
|
this.setState({readystateHandler})
|
|
|
|
}
|
2017-01-15 02:55:42 +00:00
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
<View style={styles.configRow}>
|
|
|
|
<Text>onprogress handler</Text>
|
|
|
|
<Switch
|
|
|
|
value={this.state.progressHandler}
|
2018-05-11 20:32:37 +00:00
|
|
|
onValueChange={progressHandler => this.setState({progressHandler})}
|
2017-01-15 02:55:42 +00:00
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
<View style={styles.configRow}>
|
|
|
|
<Text>download as arraybuffer</Text>
|
|
|
|
<Switch
|
|
|
|
value={this.state.arraybuffer}
|
2018-05-11 20:32:37 +00:00
|
|
|
onValueChange={arraybuffer => this.setState({arraybuffer})}
|
2017-01-15 02:55:42 +00:00
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
{button}
|
|
|
|
{readystate}
|
|
|
|
{progress}
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
wrapper: {
|
|
|
|
borderRadius: 5,
|
|
|
|
marginBottom: 5,
|
|
|
|
},
|
|
|
|
button: {
|
|
|
|
backgroundColor: '#eeeeee',
|
|
|
|
padding: 8,
|
|
|
|
},
|
|
|
|
progressBarLabel: {
|
|
|
|
marginTop: 12,
|
|
|
|
marginBottom: 8,
|
|
|
|
},
|
|
|
|
configRow: {
|
|
|
|
flexDirection: 'row',
|
|
|
|
paddingVertical: 8,
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'space-between',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = XHRExampleDownload;
|