Skip to content

Commit 814ce78

Browse files
author
Marc Mendiola
committed
Merge pull request #1 from iodine/marc.add-post
Add support for post requests
2 parents efb2151 + cb4d5f6 commit 814ce78

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

Downloader.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ typedef void (^ProgressCallback)(NSNumber*, NSNumber*);
99

1010
@property (copy) NSString* fromUrl;
1111
@property (copy) NSString* toFile;
12+
@property (copy) NSString* method;
13+
@property (copy) NSString* postString;
1214
@property (copy) DownloaderCallback callback; // Download has finished (data written)
1315
@property (copy) ErrorCallback errorCallback; // Something went wrong
1416
@property (copy) BeginCallback beginCallback; // Download has started (headers received)

Downloader.m

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,19 @@ @implementation Downloader
2222
- (void)downloadFile:(DownloadParams*)params
2323
{
2424
_params = params;
25-
25+
2626
_bytesWritten = 0;
2727

2828
NSURL* url = [NSURL URLWithString:_params.fromUrl];
2929

3030
NSMutableURLRequest* downloadRequest = [NSMutableURLRequest requestWithURL:url
3131
cachePolicy:NSURLRequestUseProtocolCachePolicy
3232
timeoutInterval:30];
33+
if ([_params.method isEqualToString: @"POST"]) {
34+
[downloadRequest setHTTPMethod:_params.method];
35+
NSString *postString = _params.postString;
36+
[downloadRequest setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
37+
}
3338

3439
[[NSFileManager defaultManager] createFileAtPath:_params.toFile contents:nil attributes:nil];
3540

@@ -61,7 +66,7 @@ - (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLRespons
6166

6267
_statusCode = [NSNumber numberWithLong:httpUrlResponse.statusCode];
6368
_contentLength = [NSNumber numberWithLong: httpUrlResponse.expectedContentLength];
64-
69+
6570
return _params.beginCallback(_statusCode, _contentLength, httpUrlResponse.allHeaderFields);
6671
}
6772

FS.common.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ var RNFS = {
8383
})
8484
.catch(convertError);
8585
},
86-
86+
8787
exists(filepath) {
8888
return _exists(filepath)
8989
.catch(convertError);
@@ -150,7 +150,7 @@ var RNFS = {
150150
.catch(convertError);
151151
},
152152

153-
downloadFile(fromUrl, toFile, begin, progress) {
153+
downloadFile(fromUrl, toFile, method = 'GET', paramString = '', begin, progress) {
154154
var jobId = getJobId();
155155
var subscriptionIos, subscriptionAndroid;
156156

@@ -173,7 +173,7 @@ var RNFS = {
173173
subscriptionAndroid = DeviceEventEmitter.addListener('DownloadProgress-' + jobId, progress);
174174
}
175175

176-
return _downloadFile(fromUrl, toFile, jobId)
176+
return _downloadFile(fromUrl, toFile, method, paramString, jobId)
177177
.then(res => {
178178
if (subscriptionIos) subscriptionIos.remove();
179179
if (subscriptionAndroid) subscriptionAndroid.remove();
@@ -189,7 +189,6 @@ var RNFS = {
189189
MainBundlePath: RNFSManager.MainBundlePath,
190190
CachesDirectoryPath: RNFSManager.NSCachesDirectoryPath,
191191
DocumentDirectoryPath: RNFSManager.NSDocumentDirectoryPath,
192-
ExternalDirectoryPath: RNFSManager.NSExternalDirectoryPath,
193192
LibraryDirectoryPath: RNFSManager.NSLibraryDirectoryPath,
194193
PicturesDirectoryPath: RNFSManager.NSPicturesDirectoryPath
195194
};

RNFSManager.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ - (dispatch_queue_t)methodQueue
174174

175175
RCT_EXPORT_METHOD(downloadFile:(NSString *)urlStr
176176
filepath:(NSString *)filepath
177+
method:(NSString *)method
178+
postString:(NSString *)postString
177179
jobId:(nonnull NSNumber *)jobId
178180
callback:(RCTResponseSenderBlock)callback)
179181
{
@@ -182,6 +184,8 @@ - (dispatch_queue_t)methodQueue
182184

183185
params.fromUrl = urlStr;
184186
params.toFile = filepath;
187+
params.method = method;
188+
params.postString = postString;
185189

186190
params.callback = ^(NSNumber* statusCode, NSNumber* bytesWritten) {
187191
NSMutableDictionary* result = [[NSMutableDictionary alloc] initWithDictionary: @{@"jobId": jobId,

0 commit comments

Comments
 (0)