Skip to content

Commit 309bec5

Browse files
authored
Merge pull request #281 from msbit/feat/ios-chunked-download-progress
feat(ios): replicate Android chunked download reporting
2 parents 72bc09d + 756c74d commit 309bec5

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

ios/ReactNativeBlobUtilRequest.mm

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -349,17 +349,35 @@ - (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dat
349349
return;
350350
}
351351

352-
NSNumber * now =[NSNumber numberWithFloat:((float)receivedBytes/(float)expectedBytes)];
352+
// For non-chunked download, progress is received / expected
353+
// For chunked download, progress can be either 0 (started) or 1 (ended)
354+
NSNumber *now;
355+
if (expectedBytes != NSURLResponseUnknownLength) {
356+
now = [NSNumber numberWithFloat:((float)receivedBytes/(float)expectedBytes)];
357+
} else {
358+
now = @0;
359+
}
353360

354361
if ([self.progressConfig shouldReport:now]) {
355-
[self.baseModule emitEventDict :EVENT_PROGRESS
356-
body:@{
362+
NSDictionary *body;
363+
if (expectedBytes == NSURLResponseUnknownLength) {
364+
// For chunked downloads
365+
body = @{
366+
@"taskId": taskId,
367+
@"written": [NSString stringWithFormat:@"%d", 0],
368+
@"total": [NSString stringWithFormat:@"%lld", (long long) expectedBytes],
369+
@"chunk": chunkString,
370+
};
371+
} else {
372+
// For non-chunked downloads
373+
body = @{
357374
@"taskId": taskId,
358375
@"written": [NSString stringWithFormat:@"%lld", (long long) receivedBytes],
359376
@"total": [NSString stringWithFormat:@"%lld", (long long) expectedBytes],
360-
@"chunk": chunkString
361-
}
362-
];
377+
@"chunk": chunkString,
378+
};
379+
}
380+
[self.baseModule emitEventDict:EVENT_PROGRESS body:body];
363381
}
364382
}
365383

@@ -392,6 +410,14 @@ - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCom
392410
} else {
393411
errMsg = [error localizedDescription];
394412
}
413+
} else if ([self.progressConfig shouldReport:@1] && expectedBytes == NSURLResponseUnknownLength) {
414+
// For chunked downloads
415+
[self.baseModule emitEventDict:EVENT_PROGRESS body:@{
416+
@"taskId": taskId,
417+
@"written": [NSString stringWithFormat:@"%lld", (long long) receivedBytes],
418+
@"total": [NSString stringWithFormat:@"%lld", (long long) receivedBytes],
419+
@"chunk": @"",
420+
}];
395421
}
396422

397423
if (respFile) {

0 commit comments

Comments
 (0)