@@ -349,17 +349,35 @@ - (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dat
349
349
return ;
350
350
}
351
351
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
+ }
353
360
354
361
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 = @{
357
374
@" taskId" : taskId,
358
375
@" written" : [NSString stringWithFormat: @" %lld " , (long long ) receivedBytes],
359
376
@" 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];
363
381
}
364
382
}
365
383
@@ -392,6 +410,14 @@ - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCom
392
410
} else {
393
411
errMsg = [error localizedDescription ];
394
412
}
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
+ }];
395
421
}
396
422
397
423
if (respFile) {
0 commit comments