Bläddra i källkod

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

Ben Hsieh 8 år sedan
förälder
incheckning
710d60de37
3 ändrade filer med 11 tillägg och 9 borttagningar
  1. 8
    6
      src/ios/RNFetchBlob/RNFetchBlob.m
  2. 1
    1
      src/ios/RNFetchBlobNetwork.h
  3. 2
    2
      src/ios/RNFetchBlobNetwork.m

+ 8
- 6
src/ios/RNFetchBlob/RNFetchBlob.m Visa fil

@@ -129,7 +129,7 @@ RCT_EXPORT_METHOD(fetchBlobForm:(NSDictionary *)options
129 129
         
130 130
         // send HTTP request
131 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,7 +147,6 @@ RCT_EXPORT_METHOD(fetchBlob:(NSDictionary *)options
147 147
                                                  URLWithString: url]];
148 148
     
149 149
     NSMutableDictionary *mheaders = [[NSMutableDictionary alloc] initWithDictionary:[RNFetchBlobNetwork normalizeHeaders:headers]];
150
-    
151 150
     // move heavy task to another thread
152 151
     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
153 152
         NSMutableData * blobData;
@@ -159,12 +158,15 @@ RCT_EXPORT_METHOD(fetchBlob:(NSDictionary *)options
159 158
                 // when body is a string contains file path prefix, try load file from the path
160 159
                 if([body hasPrefix:self.filePathPrefix]) {
161 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 164
                 // otherwise convert it as BASE64 data string
165
-                else
165
+                else {
166 166
                     blobData = [[NSData alloc] initWithBase64EncodedString:body options:0];
167
-                [request setHTTPBody:blobData];
167
+                    [request setHTTPBody:blobData];
168
+                }
169
+                
168 170
                 [mheaders setValue:@"application/octet-stream" forKey:@"content-type"];
169 171
                 
170 172
             }
@@ -175,7 +177,7 @@ RCT_EXPORT_METHOD(fetchBlob:(NSDictionary *)options
175 177
         
176 178
         // send HTTP request
177 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 Visa fil

@@ -37,7 +37,7 @@
37 37
 - (void) sendRequest;
38 38
 
39 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 43
 @end

+ 2
- 2
src/ios/RNFetchBlobNetwork.m Visa fil

@@ -51,7 +51,7 @@
51 51
 }
52 52
 
53 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 55
     self.taskId = taskId;
56 56
     self.respData = [[NSMutableData alloc] initWithLength:0];
57 57
     self.callback = callback;
@@ -106,8 +106,8 @@
106 106
     }
107 107
     // base64 response
108 108
     else {
109
-        
110 109
         NSURLSessionUploadTask * task =
110
+        
111 111
         [session dataTaskWithRequest:req completionHandler:^(NSData * _Nullable resp, NSURLResponse * _Nullable response, NSError * _Nullable error) {
112 112
             if(error != nil) {
113 113
                 callback(@[[error localizedDescription]]);