Skip to content

Commit

Permalink
Updated upload callback to return a dictionary with keys: status, data.
Browse files Browse the repository at this point in the history
Usage:

RNUploader.upload( opts, ( err, res )=>{
	if( err ){
		console.log(err);
		return;
	}

	let status = res.status;
	let responseString = res.data;
	let json = JSON.parse( responseString );

	console.log('upload complete with status ' + status);
});
  • Loading branch information
aroth committed Dec 21, 2015
1 parent 3e0a847 commit 5895e73
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ componentDidMount(){
let bytesTotal = data.totalBytesExpectedToWrite;
let progress = data.progress;

console.log( "upload progress: " + data.progress + "%");
console.log( "upload progress: " + progress + "%");
});
}
```
Expand Down Expand Up @@ -60,14 +60,17 @@ doUpload(){
params: { 'user_id': 1 }, // optional
};

RNUploader.upload( opts, ( err, data )=>{
RNUploader.upload( opts, ( err, res )=>{
if( err ){
console.log(err);
return;
}

console.log('upload complete');
console.log('response string: ' + data);
let status = res.status;
let responseString = res.data;
let json = JSON.parse( responseString );

console.log('upload complete with status ' + status);
});
}

Expand All @@ -80,7 +83,7 @@ Inspired by similiar projects:
...with noteable enhancements:
* uploads are performed asynchronously on the native side
* progress reporting
* packaged as an library
* packaged as a static library
* support for multiple files at a time
* support for files from the assets library, base64 `data:` or `file:` paths
* no external dependencies (ie: AFNetworking)
Expand Down
15 changes: 7 additions & 8 deletions RNUploader/RNUploader.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

@interface RNUploader : NSObject <RCTBridgeModule, NSURLConnectionDelegate, NSURLConnectionDataDelegate>
@property NSMutableData *responseData;
@property NSInteger responseStatusCode;

@property NSMutableURLRequest *request;
@property NSMutableData *requestBody;
@property NSMutableArray *files;
Expand All @@ -19,8 +21,6 @@ @interface RNUploader : NSObject <RCTBridgeModule, NSURLConnectionDelegate, NSUR
@property dispatch_group_t fgroup;
@end



@implementation RNUploader

@synthesize bridge = _bridge;
Expand Down Expand Up @@ -59,7 +59,6 @@ @implementation RNUploader
});
}


//
// Action Methods
//
Expand Down Expand Up @@ -183,7 +182,6 @@ - (void)sendRequest {
[connection start];
}


//
// Helpers
//
Expand All @@ -208,7 +206,6 @@ - (NSString *)mimeTypeForPath:(NSString *)filepath
return contentType;
}


//
// Delegate Methods
//
Expand All @@ -224,7 +221,7 @@ - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)err

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[self.bridge.eventDispatcher sendDeviceEventWithName:@"RNUploaderDidReceiveResponse" body:nil];

self.responseStatusCode = [(NSHTTPURLResponse *)response statusCode];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
Expand All @@ -236,8 +233,10 @@ - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *responseString = [[NSString alloc] initWithData:self.responseData encoding:NSUTF8StringEncoding];
[self.bridge.eventDispatcher sendDeviceEventWithName:@"RNUploaderDataFinishLoading" body:responseString];
_callback(@[[NSNull null], responseString]);


NSDictionary *res= [[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithInteger:self.responseStatusCode],@"status",responseString,@"data",nil];

_callback(@[[NSNull null], res]);
}

- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "git",
"url": "git+https://github.com/aroth/react-native-uploader.git"
},
"version": "0.0.2",
"version": "0.0.3",
"description": "A React Native module to upload files and camera roll assets. Supports progress notification.",
"author": {
"name": "Adam Roth",
Expand Down

0 comments on commit 5895e73

Please sign in to comment.