Browse Source

Add additional parameter support for #141

Ben Hsieh 8 years ago
parent
commit
b239dfaa8c

+ 2
- 1
src/android/src/main/java/com/RNFetchBlob/Response/RNFetchBlobFileResp.java View File

41
         assert path != null;
41
         assert path != null;
42
         this.mPath = path;
42
         this.mPath = path;
43
         if (path != null) {
43
         if (path != null) {
44
+            boolean appendToExistingFile = path.contains("?append=true");
44
             File f = new File(path);
45
             File f = new File(path);
45
             if(f.exists() == false)
46
             if(f.exists() == false)
46
                 f.createNewFile();
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 View File

220
     if ([response respondsToSelector:@selector(allHeaderFields)])
220
     if ([response respondsToSelector:@selector(allHeaderFields)])
221
     {
221
     {
222
         NSDictionary *headers = [httpResponse allHeaderFields];
222
         NSDictionary *headers = [httpResponse allHeaderFields];
223
+        if(expectedBytes < 0)
224
+        {
225
+            expectedBytes = [[headers valueForKey:@"Content-Length"] intValue];
226
+            
227
+        }
223
         NSString * respCType = [[RNFetchBlobReqBuilder getHeaderIgnoreCases:@"Content-Type" fromHeaders:headers] lowercaseString];
228
         NSString * respCType = [[RNFetchBlobReqBuilder getHeaderIgnoreCases:@"Content-Type" fromHeaders:headers] lowercaseString];
224
         if(respCType != nil)
229
         if(respCType != nil)
225
         {
230
         {
282
         @try{
287
         @try{
283
             NSFileManager * fm = [NSFileManager defaultManager];
288
             NSFileManager * fm = [NSFileManager defaultManager];
284
             NSString * folder = [destPath stringByDeletingLastPathComponent];
289
             NSString * folder = [destPath stringByDeletingLastPathComponent];
285
-            if(![fm fileExistsAtPath:folder]) {
290
+            if(![fm fileExistsAtPath:folder])
291
+            {
286
                 [fm createDirectoryAtPath:folder withIntermediateDirectories:YES attributes:NULL error:nil];
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
                 [fm createFileAtPath:destPath contents:[[NSData alloc] init] attributes:nil];
300
                 [fm createFileAtPath:destPath contents:[[NSData alloc] init] attributes:nil];
290
             }
301
             }
291
             writeStream = [[NSOutputStream alloc] initToFileAtPath:destPath append:YES];
302
             writeStream = [[NSOutputStream alloc] initToFileAtPath:destPath append:YES];