1. Return statusCode, headers for uploadFiles method

2. Fix README
This commit is contained in:
Ruoyu Sun 2016-06-15 17:51:43 +08:00
parent 592eb73d1d
commit 951a93a33a
4 changed files with 31 additions and 23 deletions

View File

@ -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

View File

@ -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]);
};

View File

@ -1,7 +1,7 @@
#import <Foundation/Foundation.h>
#import <MobileCoreServices/MobileCoreServices.h>
typedef void (^UploadCompleteCallback)(NSString*);
typedef void (^UploadCompleteCallback)(NSString*, NSURLResponse *);
typedef void (^UploadErrorCallback)(NSError*);
typedef void (^UploadBeginCallback)();
typedef void (^UploadProgressCallback)(NSNumber*, NSNumber*);

View File

@ -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];