Browse Source

Change file upload implementation of IOS so that it can upload large file

Ben Hsieh 8 years ago
parent
commit
710d60de37

+ 8
- 6
src/ios/RNFetchBlob/RNFetchBlob.m View File

129
         
129
         
130
         // send HTTP request
130
         // send HTTP request
131
         RNFetchBlobNetwork * utils = [[RNFetchBlobNetwork alloc] init];
131
         RNFetchBlobNetwork * utils = [[RNFetchBlobNetwork alloc] init];
132
-        [utils sendRequest:options bridge:self.bridge taskId:taskId withRequest:request withData:postData callback:callback];
132
+        [utils sendRequest:options bridge:self.bridge taskId:taskId withRequest:request callback:callback];
133
     });
133
     });
134
 }
134
 }
135
 
135
 
147
                                                  URLWithString: url]];
147
                                                  URLWithString: url]];
148
     
148
     
149
     NSMutableDictionary *mheaders = [[NSMutableDictionary alloc] initWithDictionary:[RNFetchBlobNetwork normalizeHeaders:headers]];
149
     NSMutableDictionary *mheaders = [[NSMutableDictionary alloc] initWithDictionary:[RNFetchBlobNetwork normalizeHeaders:headers]];
150
-    
151
     // move heavy task to another thread
150
     // move heavy task to another thread
152
     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
151
     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
153
         NSMutableData * blobData;
152
         NSMutableData * blobData;
159
                 // when body is a string contains file path prefix, try load file from the path
158
                 // when body is a string contains file path prefix, try load file from the path
160
                 if([body hasPrefix:self.filePathPrefix]) {
159
                 if([body hasPrefix:self.filePathPrefix]) {
161
                     NSString * orgPath = [body substringFromIndex:[self.filePathPrefix length]];
160
                     NSString * orgPath = [body substringFromIndex:[self.filePathPrefix length]];
162
-                    blobData = [[NSData alloc] initWithContentsOfFile:orgPath];
161
+                    [request setHTTPBodyStream: [NSInputStream inputStreamWithFileAtPath:orgPath ]];
162
+//                    blobData = [[NSData alloc] initWithContentsOfFile:orgPath];
163
                 }
163
                 }
164
                 // otherwise convert it as BASE64 data string
164
                 // otherwise convert it as BASE64 data string
165
-                else
165
+                else {
166
                     blobData = [[NSData alloc] initWithBase64EncodedString:body options:0];
166
                     blobData = [[NSData alloc] initWithBase64EncodedString:body options:0];
167
-                [request setHTTPBody:blobData];
167
+                    [request setHTTPBody:blobData];
168
+                }
169
+                
168
                 [mheaders setValue:@"application/octet-stream" forKey:@"content-type"];
170
                 [mheaders setValue:@"application/octet-stream" forKey:@"content-type"];
169
                 
171
                 
170
             }
172
             }
175
         
177
         
176
         // send HTTP request
178
         // send HTTP request
177
         RNFetchBlobNetwork * utils = [[RNFetchBlobNetwork alloc] init];
179
         RNFetchBlobNetwork * utils = [[RNFetchBlobNetwork alloc] init];
178
-        [utils sendRequest:options bridge:self.bridge taskId:taskId withRequest:request withData:blobData callback:callback];
180
+        [utils sendRequest:options bridge:self.bridge taskId:taskId withRequest:request callback:callback];
179
     });
181
     });
180
 }
182
 }
181
 
183
 

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

37
 - (void) sendRequest;
37
 - (void) sendRequest;
38
 
38
 
39
 + (NSMutableDictionary *) normalizeHeaders:(NSDictionary *)headers;
39
 + (NSMutableDictionary *) normalizeHeaders:(NSDictionary *)headers;
40
-- (void) sendRequest:(NSDictionary *)options bridge:(RCTBridge *)bridgeRef taskId:(NSString *)taskId withRequest:(NSURLRequest *)req withData:( NSData * _Nullable )data callback:(RCTResponseSenderBlock) callback;
40
+- (void) sendRequest:(NSDictionary *)options bridge:(RCTBridge *)bridgeRef taskId:(NSString *)taskId withRequest:(NSURLRequest *)req callback:(RCTResponseSenderBlock) callback;
41
 
41
 
42
 
42
 
43
 @end
43
 @end

+ 2
- 2
src/ios/RNFetchBlobNetwork.m View File

51
 }
51
 }
52
 
52
 
53
 // send HTTP request
53
 // send HTTP request
54
-- (void) sendRequest:(NSDictionary *)options bridge:(RCTBridge *)bridgeRef taskId:(NSString *)taskId withRequest:(NSURLRequest *)req withData:( NSData * _Nullable )data callback:(RCTResponseSenderBlock) callback {
54
+- (void) sendRequest:(NSDictionary *)options bridge:(RCTBridge *)bridgeRef taskId:(NSString *)taskId withRequest:(NSURLRequest *)req callback:(RCTResponseSenderBlock) callback {
55
     self.taskId = taskId;
55
     self.taskId = taskId;
56
     self.respData = [[NSMutableData alloc] initWithLength:0];
56
     self.respData = [[NSMutableData alloc] initWithLength:0];
57
     self.callback = callback;
57
     self.callback = callback;
106
     }
106
     }
107
     // base64 response
107
     // base64 response
108
     else {
108
     else {
109
-        
110
         NSURLSessionUploadTask * task =
109
         NSURLSessionUploadTask * task =
110
+        
111
         [session dataTaskWithRequest:req completionHandler:^(NSData * _Nullable resp, NSURLResponse * _Nullable response, NSError * _Nullable error) {
111
         [session dataTaskWithRequest:req completionHandler:^(NSData * _Nullable resp, NSURLResponse * _Nullable response, NSError * _Nullable error) {
112
             if(error != nil) {
112
             if(error != nil) {
113
                 callback(@[[error localizedDescription]]);
113
                 callback(@[[error localizedDescription]]);