diff --git a/README.md b/README.md index 7ac774c..b3fd835 100644 --- a/README.md +++ b/README.md @@ -198,16 +198,6 @@ var files = [ filetype: 'audio/x-m4a' } ]; -// create an object of options -var options = { - method: 'POST', - headers: { - 'Accept': 'application/json', - }, - fields: { - 'hello': 'world', - } -}; var uploadBegin = (response) => { var jobId = response.jobId; @@ -220,9 +210,25 @@ var uploadProgress = (response) => { }; // upload files -RNFS.uploadFiles(uploadUrl, files, options, uploadBegin, uploadProgress) +RNFS.uploadFiles({ + toUrl: uploadUrl, + files: files, + method: 'POST', + headers: { + 'Accept': 'application/json', + }, + fields: { + 'hello': 'world', + }, + begin: uploadBegin, + progress: uploadProgress + }) .then((response) => { - console.log('FILES UPLOADED!'); + if (response.statusCode == 200) { + console.log('FILES UPLOADED!'); // response.statusCode, response.headers, response.body + } else { + console.log('SERVER ERROR'); + } }) .catch((err) => { if(err.description === "cancelled") { @@ -358,7 +364,7 @@ Abort the current download job with this ID. The partial file will remain on the ``` { - url (String) - URL to upload file to + toUrl (String) - URL to upload file to files (Array) - An array of objects with the file information to be uploaded. method (String) - (Optional) Default is 'POST', supports 'POST' and 'PUT' headers (Object) - (Optional) An object of headers to be passed to the server diff --git a/RNFSManager.m b/RNFSManager.m index cde4fd5..4d1fbc1 100644 --- a/RNFSManager.m +++ b/RNFSManager.m @@ -250,9 +250,14 @@ RCT_EXPORT_METHOD(uploadFiles:(NSDictionary *)options params.fields = fields; params.method = method; - params.completeCallback = ^(NSString* response) { + params.completeCallback = ^(NSString* body, NSURLResponse *resp) { + NSMutableDictionary* result = [[NSMutableDictionary alloc] initWithDictionary: @{@"jobId": jobId, - @"response": response}]; + @"body": body}]; + if ([resp isKindOfClass:[NSHTTPURLResponse class]]) { + [result setValue:((NSHTTPURLResponse *)resp).allHeaderFields forKey:@"headers"]; + [result setValue:[NSNumber numberWithUnsignedInteger:((NSHTTPURLResponse *)resp).statusCode] forKey:@"statusCode"]; + } return callback(@[[NSNull null], result]); }; diff --git a/Uploader.h b/Uploader.h index 17aca40..056c89d 100644 --- a/Uploader.h +++ b/Uploader.h @@ -1,7 +1,7 @@ #import #import -typedef void (^UploadCompleteCallback)(NSString*); +typedef void (^UploadCompleteCallback)(NSString*, NSURLResponse *); typedef void (^UploadErrorCallback)(NSError*); typedef void (^UploadBeginCallback)(); typedef void (^UploadProgressCallback)(NSNumber*, NSNumber*); diff --git a/Uploader.m b/Uploader.m index 89fa11b..d302af6 100644 --- a/Uploader.m +++ b/Uploader.m @@ -98,7 +98,10 @@ NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration]; NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:(id)self delegateQueue:[NSOperationQueue mainQueue]]; - _task = [session dataTaskWithRequest:req]; + _task = [session dataTaskWithRequest:req completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { + NSString * str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; + return _params.completeCallback(str, response); + }]; [_task resume]; _params.beginCallback(); } @@ -133,12 +136,6 @@ return _params.progressCallback([NSNumber numberWithLongLong:totalBytesExpectedToSend], [NSNumber numberWithLongLong:totalBytesSent]); } -- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data -{ - NSString * str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; - return _params.completeCallback(str); -} - - (void)stopUpload { [_task cancel];