Skip to content
This repository was archived by the owner on Mar 16, 2019. It is now read-only.

Commit 710d60d

Browse files
committed
Change file upload implementation of IOS so that it can upload large file
1 parent 55c5347 commit 710d60d

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

src/ios/RNFetchBlob/RNFetchBlob.m

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ - (NSDictionary *)constantsToExport
129129

130130
// send HTTP request
131131
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];
133133
});
134134
}
135135

@@ -147,7 +147,6 @@ - (NSDictionary *)constantsToExport
147147
URLWithString: url]];
148148

149149
NSMutableDictionary *mheaders = [[NSMutableDictionary alloc] initWithDictionary:[RNFetchBlobNetwork normalizeHeaders:headers]];
150-
151150
// move heavy task to another thread
152151
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
153152
NSMutableData * blobData;
@@ -159,12 +158,15 @@ - (NSDictionary *)constantsToExport
159158
// when body is a string contains file path prefix, try load file from the path
160159
if([body hasPrefix:self.filePathPrefix]) {
161160
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];
163163
}
164164
// otherwise convert it as BASE64 data string
165-
else
165+
else {
166166
blobData = [[NSData alloc] initWithBase64EncodedString:body options:0];
167-
[request setHTTPBody:blobData];
167+
[request setHTTPBody:blobData];
168+
}
169+
168170
[mheaders setValue:@"application/octet-stream" forKey:@"content-type"];
169171

170172
}
@@ -175,7 +177,7 @@ - (NSDictionary *)constantsToExport
175177

176178
// send HTTP request
177179
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];
179181
});
180182
}
181183

src/ios/RNFetchBlobNetwork.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
- (void) sendRequest;
3838

3939
+ (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;
4141

4242

4343
@end

src/ios/RNFetchBlobNetwork.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ + (NSMutableDictionary *) normalizeHeaders:(NSDictionary *)headers {
5151
}
5252

5353
// 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 {
5555
self.taskId = taskId;
5656
self.respData = [[NSMutableData alloc] initWithLength:0];
5757
self.callback = callback;
@@ -106,8 +106,8 @@ - (void) sendRequest:(NSDictionary *)options bridge:(RCTBridge *)bridgeRef taskI
106106
}
107107
// base64 response
108108
else {
109-
110109
NSURLSessionUploadTask * task =
110+
111111
[session dataTaskWithRequest:req completionHandler:^(NSData * _Nullable resp, NSURLResponse * _Nullable response, NSError * _Nullable error) {
112112
if(error != nil) {
113113
callback(@[[error localizedDescription]]);

0 commit comments

Comments
 (0)