Просмотр исходного кода

Add additional parameter support for #141

Ben Hsieh 7 лет назад
Родитель
Сommit
b239dfaa8c

+ 2
- 1
src/android/src/main/java/com/RNFetchBlob/Response/RNFetchBlobFileResp.java Просмотреть файл

@@ -41,10 +41,11 @@ public class RNFetchBlobFileResp extends ResponseBody {
41 41
         assert path != null;
42 42
         this.mPath = path;
43 43
         if (path != null) {
44
+            boolean appendToExistingFile = path.contains("?append=true");
44 45
             File f = new File(path);
45 46
             if(f.exists() == false)
46 47
                 f.createNewFile();
47
-            ofStream = new FileOutputStream(new File(path));
48
+            ofStream = new FileOutputStream(new File(path), appendToExistingFile);
48 49
         }
49 50
     }
50 51
 

+ 13
- 2
src/ios/RNFetchBlobNetwork.m Просмотреть файл

@@ -220,6 +220,11 @@ NSOperationQueue *taskQueue;
220 220
     if ([response respondsToSelector:@selector(allHeaderFields)])
221 221
     {
222 222
         NSDictionary *headers = [httpResponse allHeaderFields];
223
+        if(expectedBytes < 0)
224
+        {
225
+            expectedBytes = [[headers valueForKey:@"Content-Length"] intValue];
226
+            
227
+        }
223 228
         NSString * respCType = [[RNFetchBlobReqBuilder getHeaderIgnoreCases:@"Content-Type" fromHeaders:headers] lowercaseString];
224 229
         if(respCType != nil)
225 230
         {
@@ -282,10 +287,16 @@ NSOperationQueue *taskQueue;
282 287
         @try{
283 288
             NSFileManager * fm = [NSFileManager defaultManager];
284 289
             NSString * folder = [destPath stringByDeletingLastPathComponent];
285
-            if(![fm fileExistsAtPath:folder]) {
290
+            if(![fm fileExistsAtPath:folder])
291
+            {
286 292
                 [fm createDirectoryAtPath:folder withIntermediateDirectories:YES attributes:NULL error:nil];
287 293
             }
288
-            if (![fm fileExistsAtPath:destPath]) {
294
+            BOOL appendToExistingFile = [destPath RNFBContainsString:@"?append=true"];
295
+            // For solving #141 append response data if the file already exists
296
+            // base on PR#139 @kejinliang
297
+            if (appendToExistingFile && ![fm fileExistsAtPath:destPath])
298
+            {
299
+                destPath = [destPath stringByReplacingOccurrencesOfString:@"?append=true" withString:@""];
289 300
                 [fm createFileAtPath:destPath contents:[[NSData alloc] init] attributes:nil];
290 301
             }
291 302
             writeStream = [[NSOutputStream alloc] initToFileAtPath:destPath append:YES];