浏览代码

Apply fix to #264

Ben Hsieh 7 年前
父节点
当前提交
6520e0993d
共有 1 个文件被更改,包括 22 次插入1 次删除
  1. 22
    1
      src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java

+ 22
- 1
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java 查看文件

@@ -500,11 +500,32 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
500 500
                     // It uses customized response body which is able to report download progress
501 501
                     // and write response data to destination path.
502 502
                     resp.body().bytes();
503
+
503 504
                 } catch (Exception ignored) {
504 505
 //                    ignored.printStackTrace();
505 506
                 }
506 507
                 this.destPath = this.destPath.replace("?append=true", "");
507
-                callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_PATH, this.destPath);
508
+
509
+                try {
510
+                    long expectedLength = resp.body().contentLength();
511
+                    // when response contains Content-Length, check if the stream length is correct
512
+                    if(expectedLength > 0) {
513
+                        long actualLength = new File(this.destPath).length();
514
+                        if(actualLength != expectedLength) {
515
+                            callback.invoke("RNFetchBlob failed to write data to storage : expected " + expectedLength + " bytes but got " + actualLength + " bytes", null);
516
+                        }
517
+                        else {
518
+                            callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_PATH, this.destPath);
519
+                        }
520
+                    }
521
+                    else {
522
+                        callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_PATH, this.destPath);
523
+                    }
524
+                }
525
+                catch (Exception err) {
526
+                    callback.invoke(err.getMessage());
527
+                    err.printStackTrace();
528
+                }
508 529
                 break;
509 530
             default:
510 531
                 try {