Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix onprogress not firing when Content-Length is not available for XMLHttpRequest #44899

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/react-native/Libraries/Network/RCTNetworkTask.mm
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ - (void)URLRequest:(id)requestToken didReceiveData:(NSData *)data
incrementalDataBlock(data, length, total);
}];
}
if (_downloadProgressBlock && total > 0) {
if (_downloadProgressBlock) {
RCTURLRequestProgressBlock downloadProgressBlock = _downloadProgressBlock;
[self dispatchCallback:^{
downloadProgressBlock(length, total);
Expand Down
33 changes: 26 additions & 7 deletions packages/rn-tester/js/examples/XHR/XHRExampleDownload.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class XHRExampleDownload extends React.Component<{...}, Object> {
readystateHandler: false,
progressHandler: true,
arraybuffer: false,
chunked: false,
};

xhr: ?XMLHttpRequest = null;
Expand Down Expand Up @@ -107,12 +108,19 @@ class XHRExampleDownload extends React.Component<{...}, Object> {
Alert.alert('Error', xhr.responseText);
}
};
xhr.open(
'GET',
'http://aleph.gutenberg.org/cache/epub/100/pg100-images.html.utf8',
);
// Avoid gzip so we can actually show progress
xhr.setRequestHeader('Accept-Encoding', '');
if (this.state.chunked) {
xhr.open(
'GET',
'https://filesamples.com/samples/ebook/azw3/Around%20the%20World%20in%2028%20Languages.azw3',
);
} else {
xhr.open(
'GET',
'http://aleph.gutenberg.org/cache/epub/100/pg100-images.html.utf8',
);
// Avoid gzip so we can actually show progress
xhr.setRequestHeader('Accept-Encoding', '');
}
xhr.send();

this.setState({downloading: true});
Expand All @@ -133,7 +141,11 @@ class XHRExampleDownload extends React.Component<{...}, Object> {
) : (
<TouchableHighlight style={styles.wrapper} onPress={this._download}>
<View style={styles.button}>
<Text>Download 7MB Text File</Text>
<Text>
{this.state.chunked
? 'Download 10MB File'
: 'Download 19KB pdf File'}
</Text>
</View>
</TouchableHighlight>
);
Expand Down Expand Up @@ -188,6 +200,13 @@ class XHRExampleDownload extends React.Component<{...}, Object> {
onValueChange={arraybuffer => this.setState({arraybuffer})}
/>
</View>
<View style={styles.configRow}>
<Text>transfer-encoding: chunked</Text>
<Switch
value={this.state.chunked}
onValueChange={chunked => this.setState({chunked})}
/>
</View>
{button}
{readystate}
{progress}
Expand Down