Browse Source

Fix file upload total byte bug

Ben Hsieh 8 years ago
parent
commit
0bd9e0efa0

+ 4
- 4
src/ios/RNFetchBlob/RNFetchBlob.m View File

@@ -63,10 +63,10 @@ RCT_EXPORT_METHOD(fetchBlobForm:(NSDictionary *)options
63 63
                   callback:(RCTResponseSenderBlock)callback)
64 64
 {
65 65
     
66
-    [RNFetchBlobReqBuilder buildMultipartRequest:options taskId:taskId method:method url:url headers:headers form:form onComplete:^(NSURLRequest *req) {
66
+    [RNFetchBlobReqBuilder buildMultipartRequest:options taskId:taskId method:method url:url headers:headers form:form onComplete:^(NSURLRequest *req, long bodyLength) {
67 67
         // send HTTP request
68 68
         RNFetchBlobNetwork * utils = [[RNFetchBlobNetwork alloc] init];
69
-        [utils sendRequest:options bridge:self.bridge taskId:taskId withRequest:req callback:callback];
69
+        [utils sendRequest:options contentLength:bodyLength bridge:self.bridge taskId:taskId withRequest:req callback:callback];
70 70
         utils = nil;
71 71
     }];
72 72
     
@@ -80,10 +80,10 @@ RCT_EXPORT_METHOD(fetchBlob:(NSDictionary *)options
80 80
                   headers:(NSDictionary *)headers
81 81
                   body:(NSString *)body callback:(RCTResponseSenderBlock)callback)
82 82
 {
83
-    [RNFetchBlobReqBuilder buildOctetRequest:options taskId:taskId method:method url:url headers:headers body:body onComplete:^(NSURLRequest *req) {
83
+    [RNFetchBlobReqBuilder buildOctetRequest:options taskId:taskId method:method url:url headers:headers body:body onComplete:^(NSURLRequest *req, long bodyLength) {
84 84
         // send HTTP request
85 85
         RNFetchBlobNetwork * utils = [[RNFetchBlobNetwork alloc] init];
86
-        [utils sendRequest:options bridge:self.bridge taskId:taskId withRequest:req callback:callback];
86
+        [utils sendRequest:options contentLength:bodyLength bridge:self.bridge taskId:taskId withRequest:req callback:callback];
87 87
         utils = nil;
88 88
     }];
89 89
     

+ 6
- 4
src/ios/RNFetchBlobFS.m View File

@@ -283,9 +283,10 @@ NSMutableDictionary *fileStreams = nil;
283 283
         [[self class] getPathFromUri:path completionHandler:^(NSString *path, ALAssetRepresentation *asset) {
284 284
             NSData * fileContent;
285 285
             NSError * err;
286
+            Byte * buffer;
286 287
             if(asset != nil)
287 288
             {
288
-                Byte * buffer = malloc(asset.size);
289
+                buffer = malloc(asset.size);
289 290
                 [asset getBytes:buffer fromOffset:0 length:asset.size error:&err];
290 291
                 if(err != nil)
291 292
                 {
@@ -293,8 +294,6 @@ NSMutableDictionary *fileStreams = nil;
293 294
                     return;
294 295
                 }
295 296
                 fileContent = [NSData dataWithBytes:buffer length:asset.size];
296
-                if(onComplete != nil)
297
-                    onComplete(fileContent);
298 297
                 free(buffer);
299 298
             }
300 299
             else
@@ -305,7 +304,10 @@ NSMutableDictionary *fileStreams = nil;
305 304
                     return;
306 305
                 }
307 306
                 fileContent = [NSData dataWithContentsOfFile:path];
307
+                
308 308
             }
309
+            if(onComplete != nil)
310
+                onComplete(fileContent);
309 311
             
310 312
             if([[encoding lowercaseString] isEqualToString:@"utf8"]) {
311 313
                 if(resolve != nil)
@@ -329,7 +331,7 @@ NSMutableDictionary *fileStreams = nil;
329 331
     @catch(NSException * e)
330 332
     {
331 333
         if(reject != nil)
332
-        reject(@"RNFetchBlobFS readFile error", @"error", [e description]);
334
+            reject(@"RNFetchBlobFS readFile error", @"error", [e description]);
333 335
     }
334 336
 }
335 337
 

+ 1
- 1
src/ios/RNFetchBlobNetwork.h View File

@@ -35,7 +35,7 @@ typedef void(^DataTaskCompletionHander) (NSData * _Nullable resp, NSURLResponse
35 35
 - (void) sendRequest;
36 36
 
37 37
 + (NSMutableDictionary  * _Nullable ) normalizeHeaders:(NSDictionary * _Nullable)headers;
38
-- (void) sendRequest:(NSDictionary  * _Nullable )options bridge:(RCTBridge * _Nullable)bridgeRef taskId:(NSString * _Nullable)taskId withRequest:(NSURLRequest * _Nullable)req callback:(_Nullable RCTResponseSenderBlock) callback;
38
+- (void) sendRequest:(NSDictionary  * _Nullable )options contentLength:(long)contentLength bridge:(RCTBridge * _Nullable)bridgeRef taskId:(NSString * _Nullable)taskId withRequest:(NSURLRequest * _Nullable)req callback:(_Nullable RCTResponseSenderBlock) callback;
39 39
 
40 40
 
41 41
 @end

+ 9
- 3
src/ios/RNFetchBlobNetwork.m View File

@@ -69,7 +69,12 @@ NSOperationQueue *taskQueue;
69 69
 }
70 70
 
71 71
 // send HTTP request
72
-- (void) sendRequest:(NSDictionary  * _Nullable )options bridge:(RCTBridge * _Nullable)bridgeRef taskId:(NSString * _Nullable)taskId withRequest:(NSURLRequest * _Nullable)req callback:(_Nullable RCTResponseSenderBlock) callback
72
+- (void) sendRequest:(NSDictionary  * _Nullable )options
73
+       contentLength:(long) contentLength
74
+              bridge:(RCTBridge * _Nullable)bridgeRef
75
+              taskId:(NSString * _Nullable)taskId
76
+         withRequest:(NSURLRequest * _Nullable)req
77
+            callback:(_Nullable RCTResponseSenderBlock) callback
73 78
 {
74 79
     self.taskId = taskId;
75 80
     self.respData = [[NSMutableData alloc] initWithLength:0];
@@ -83,7 +88,7 @@ NSOperationQueue *taskQueue;
83 88
     NSString * ext = [self.options valueForKey:CONFIG_FILE_EXT];
84 89
     NSURLSession * session;
85 90
     
86
-    bodyLength = [[req HTTPBody] length];
91
+    bodyLength = contentLength;
87 92
     
88 93
     // the session trust any SSL certification
89 94
 
@@ -104,6 +109,7 @@ NSOperationQueue *taskQueue;
104 109
         respFile = NO;
105 110
     }
106 111
     NSURLSessionDataTask * task = [session dataTaskWithRequest:req];
112
+    
107 113
     [task resume];
108 114
     
109 115
     // network status indicator
@@ -191,7 +197,7 @@ NSOperationQueue *taskQueue;
191 197
      body:@{
192 198
             @"taskId": taskId,
193 199
             @"written": [NSString stringWithFormat:@"%d", totalBytesWritten],
194
-            @"total": [NSString stringWithFormat:@"%d", totalBytesExpectedToWrite]
200
+            @"total": [NSString stringWithFormat:@"%d", bodyLength]
195 201
             }
196 202
      ];
197 203
 }

+ 2
- 2
src/ios/RNFetchBlobReqBuilder.h View File

@@ -19,7 +19,7 @@
19 19
                           url:(NSString *)url
20 20
                       headers:(NSDictionary *)headers
21 21
                          form:(NSArray *)form
22
-                   onComplete:(void(^)(NSURLRequest * req))onComplete;
22
+                   onComplete:(void(^)(NSURLRequest * req, long bodyLength))onComplete;
23 23
 
24 24
 +(void) buildOctetRequest:(NSDictionary *)options
25 25
                    taskId:(NSString *)taskId
@@ -27,7 +27,7 @@
27 27
                       url:(NSString *)url
28 28
                   headers:(NSDictionary *)headers
29 29
                      body:(NSString *)body
30
-               onComplete:(void(^)(NSURLRequest * req))onComplete;
30
+               onComplete:(void(^)(NSURLRequest * req, long bodyLength))onComplete;
31 31
 
32 32
 @end
33 33
 

+ 8
- 5
src/ios/RNFetchBlobReqBuilder.m View File

@@ -28,7 +28,7 @@
28 28
                           url:(NSString *)url
29 29
                       headers:(NSDictionary *)headers
30 30
                          form:(NSArray *)form
31
-                   onComplete:(void(^)(NSURLRequest * req))onComplete
31
+                   onComplete:(void(^)(NSURLRequest * req, long bodyLength))onComplete
32 32
 {
33 33
     NSString * encodedUrl = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
34 34
     // send request
@@ -56,7 +56,7 @@
56 56
             [mheaders setValue:[NSString stringWithFormat:@"multipart/form-data; charset=utf-8; boundary=%@", boundary] forKey:@"content-type"];
57 57
             [request setHTTPMethod: method];
58 58
             [request setAllHTTPHeaderFields:mheaders];
59
-            onComplete(request);
59
+            onComplete(request, [formData length]);
60 60
         }];
61 61
         
62 62
     });
@@ -69,7 +69,7 @@
69 69
                       url:(NSString *)url
70 70
                   headers:(NSDictionary *)headers
71 71
                      body:(NSString *)body
72
-               onComplete:(void(^)(NSURLRequest * req))onComplete
72
+               onComplete:(void(^)(NSURLRequest * req, long bodyLength))onComplete
73 73
 {
74 74
     NSString * encodedUrl = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
75 75
     // send request
@@ -81,6 +81,7 @@
81 81
     // move heavy task to another thread
82 82
     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
83 83
         NSMutableData * blobData;
84
+        long size = -1;
84 85
         // if method is POST or PUT, convert data string format
85 86
         if([[method lowercaseString] isEqualToString:@"post"] || [[method lowercaseString] isEqualToString:@"put"]) {
86 87
             // generate octet-stream body
@@ -97,10 +98,11 @@
97 98
                             [mheaders setValue:@"application/octet-stream" forKey:@"content-type"];
98 99
                             [request setHTTPMethod: method];
99 100
                             [request setAllHTTPHeaderFields:mheaders];
100
-                            onComplete(request);
101
+                            onComplete(request, [content length]);
101 102
                         }];
102 103
                         return;
103 104
                     }
105
+                    size = [[[NSFileManager defaultManager] attributesOfItemAtPath:orgPath error:nil] fileSize];
104 106
                     [request setHTTPBodyStream: [NSInputStream inputStreamWithFileAtPath:orgPath ]];
105 107
                 }
106 108
                 // otherwise convert it as BASE64 data string
@@ -117,7 +119,7 @@
117 119
         [request setHTTPMethod: method];
118 120
         [request setAllHTTPHeaderFields:mheaders];
119 121
         
120
-        onComplete(request);
122
+        onComplete(request, size);
121 123
     });
122 124
 }
123 125
 
@@ -135,6 +137,7 @@
135 137
             NSString * name = [field valueForKey:@"name"];
136 138
             NSString * content = [field valueForKey:@"data"];
137 139
             NSString * contentType = [field valueForKey:@"type"];
140
+            contentType = contentType == nil ? @"application/octet-stream" : contentType;
138 141
             // field is a text field
139 142
             if([field valueForKey:@"filename"] == nil || content == [NSNull null]) {
140 143
                 [formData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];