Skip to content

Commit 5895e73

Browse files
committed
Updated upload callback to return a dictionary with keys: status, data.
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); });
1 parent 3e0a847 commit 5895e73

File tree

3 files changed

+16
-14
lines changed

3 files changed

+16
-14
lines changed

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ componentDidMount(){
3030
let bytesTotal = data.totalBytesExpectedToWrite;
3131
let progress = data.progress;
3232

33-
console.log( "upload progress: " + data.progress + "%");
33+
console.log( "upload progress: " + progress + "%");
3434
});
3535
}
3636
```
@@ -60,14 +60,17 @@ doUpload(){
6060
params: { 'user_id': 1 }, // optional
6161
};
6262

63-
RNUploader.upload( opts, ( err, data )=>{
63+
RNUploader.upload( opts, ( err, res )=>{
6464
if( err ){
6565
console.log(err);
6666
return;
6767
}
6868

69-
console.log('upload complete');
70-
console.log('response string: ' + data);
69+
let status = res.status;
70+
let responseString = res.data;
71+
let json = JSON.parse( responseString );
72+
73+
console.log('upload complete with status ' + status);
7174
});
7275
}
7376

@@ -80,7 +83,7 @@ Inspired by similiar projects:
8083
...with noteable enhancements:
8184
* uploads are performed asynchronously on the native side
8285
* progress reporting
83-
* packaged as an library
86+
* packaged as a static library
8487
* support for multiple files at a time
8588
* support for files from the assets library, base64 `data:` or `file:` paths
8689
* no external dependencies (ie: AFNetworking)

RNUploader/RNUploader.m

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
@interface RNUploader : NSObject <RCTBridgeModule, NSURLConnectionDelegate, NSURLConnectionDataDelegate>
1111
@property NSMutableData *responseData;
12+
@property NSInteger responseStatusCode;
13+
1214
@property NSMutableURLRequest *request;
1315
@property NSMutableData *requestBody;
1416
@property NSMutableArray *files;
@@ -19,8 +21,6 @@ @interface RNUploader : NSObject <RCTBridgeModule, NSURLConnectionDelegate, NSUR
1921
@property dispatch_group_t fgroup;
2022
@end
2123

22-
23-
2424
@implementation RNUploader
2525

2626
@synthesize bridge = _bridge;
@@ -59,7 +59,6 @@ @implementation RNUploader
5959
});
6060
}
6161

62-
6362
//
6463
// Action Methods
6564
//
@@ -183,7 +182,6 @@ - (void)sendRequest {
183182
[connection start];
184183
}
185184

186-
187185
//
188186
// Helpers
189187
//
@@ -208,7 +206,6 @@ - (NSString *)mimeTypeForPath:(NSString *)filepath
208206
return contentType;
209207
}
210208

211-
212209
//
213210
// Delegate Methods
214211
//
@@ -224,7 +221,7 @@ - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)err
224221

225222
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
226223
[self.bridge.eventDispatcher sendDeviceEventWithName:@"RNUploaderDidReceiveResponse" body:nil];
227-
224+
self.responseStatusCode = [(NSHTTPURLResponse *)response statusCode];
228225
}
229226

230227
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
@@ -236,8 +233,10 @@ - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
236233
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
237234
NSString *responseString = [[NSString alloc] initWithData:self.responseData encoding:NSUTF8StringEncoding];
238235
[self.bridge.eventDispatcher sendDeviceEventWithName:@"RNUploaderDataFinishLoading" body:responseString];
239-
_callback(@[[NSNull null], responseString]);
240-
236+
237+
NSDictionary *res= [[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithInteger:self.responseStatusCode],@"status",responseString,@"data",nil];
238+
239+
_callback(@[[NSNull null], res]);
241240
}
242241

243242
- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"type": "git",
55
"url": "git+https://github.com/aroth/react-native-uploader.git"
66
},
7-
"version": "0.0.2",
7+
"version": "0.0.3",
88
"description": "A React Native module to upload files and camera roll assets. Supports progress notification.",
99
"author": {
1010
"name": "Adam Roth",

0 commit comments

Comments
 (0)