From 9e892119cc24100e075cd85caec8815487835fde Mon Sep 17 00:00:00 2001 From: Sefa Ilkimen Date: Sun, 29 Sep 2019 21:43:58 +0200 Subject: [PATCH] feat(#127): adding multiple file upload - implement multi uploading files for ios - update readme --- README.md | 16 ++++++++++++---- src/ios/CordovaHttpPlugin.h | 2 +- src/ios/CordovaHttpPlugin.m | 15 +++++++++------ 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 1e26607..8694bf3 100644 --- a/README.md +++ b/README.md @@ -212,8 +212,8 @@ The options object contains following keys: * `timeout`: timeout value for the request in seconds, defaults to global timeout value * `followRedirect`: enable or disable automatically following redirects * `headers`: headers object (key value pair), will be merged with global values -* `filePath`: filePath to be used during upload and download see [uploadFile](#uploadFile) and [downloadFile](#downloadFile) for detailed information -* `name`: name to be used during upload see [uploadFile](#uploadFile) for detailed information +* `filePath`: file path(s) to be used during upload and download see [uploadFile](#uploadFile) and [downloadFile](#downloadFile) for detailed information +* `name`: name(s) to be used during upload see [uploadFile](#uploadFile) for detailed information Here's a quick example: @@ -337,13 +337,21 @@ Execute a DELETE request. Takes a URL, parameters, and headers. See the [post] Execute a HEAD request. Takes a URL, parameters, and headers. See the [post](#post) documentation for details on what is returned on success and failure. ### uploadFile -Uploads a file saved on the device. Takes a URL, parameters, headers, filePath, and the name of the parameter to pass the file along as. See the [post](#post) documentation for details on what is returned on success and failure. +Uploads one or more file(s) saved on the device. Takes a URL, parameters, headers, filePath(s), and the name(s) of the parameter to pass the file along as. See the [post](#post) documentation for details on what is returned on success and failure. ```js +// e.g. for single file +const filePath = 'file:///somepicture.jpg'; +const name = 'picture'; + +// e.g. for multiple files +const filePath = ['file:///somepicture.jpg', 'file:///somedocument.doc']; +const name = ['picture', 'document']; + cordova.plugin.http.uploadFile("https://google.com/", { id: '12', message: 'test' -}, { Authorization: 'OAuth2: token' }, 'file:///somepicture.jpg', 'picture', function(response) { +}, { Authorization: 'OAuth2: token' }, filePath, name, function(response) { console.log(response.status); }, function(response) { console.error(response.error); diff --git a/src/ios/CordovaHttpPlugin.h b/src/ios/CordovaHttpPlugin.h index e9daee1..e87a1de 100644 --- a/src/ios/CordovaHttpPlugin.h +++ b/src/ios/CordovaHttpPlugin.h @@ -10,7 +10,7 @@ - (void)put:(CDVInvokedUrlCommand*)command; - (void)patch:(CDVInvokedUrlCommand*)command; - (void)delete:(CDVInvokedUrlCommand*)command; -- (void)uploadFile:(CDVInvokedUrlCommand*)command; +- (void)uploadFiles:(CDVInvokedUrlCommand*)command; - (void)downloadFile:(CDVInvokedUrlCommand*)command; @end diff --git a/src/ios/CordovaHttpPlugin.m b/src/ios/CordovaHttpPlugin.m index 13d4369..bdbb9c4 100644 --- a/src/ios/CordovaHttpPlugin.m +++ b/src/ios/CordovaHttpPlugin.m @@ -420,20 +420,18 @@ } } -- (void)uploadFile:(CDVInvokedUrlCommand*)command { +- (void)uploadFiles:(CDVInvokedUrlCommand*)command { AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; manager.securityPolicy = securityPolicy; NSString *url = [command.arguments objectAtIndex:0]; NSDictionary *headers = [command.arguments objectAtIndex:1]; - NSString *filePath = [command.arguments objectAtIndex: 2]; - NSString *name = [command.arguments objectAtIndex: 3]; + NSArray *filePaths = [command.arguments objectAtIndex: 2]; + NSArray *names = [command.arguments objectAtIndex: 3]; NSTimeInterval timeoutInSeconds = [[command.arguments objectAtIndex:4] doubleValue]; bool followRedirect = [[command.arguments objectAtIndex:5] boolValue]; NSString *responseType = [command.arguments objectAtIndex:6]; - NSURL *fileURL = [NSURL URLWithString: filePath]; - [self setRequestHeaders: headers forManager: manager]; [self setTimeout:timeoutInSeconds forManager:manager]; [self setRedirect:followRedirect forManager:manager]; @@ -445,7 +443,12 @@ @try { [manager POST:url parameters:nil constructingBodyWithBlock:^(id formData) { NSError *error; - [formData appendPartWithFileURL:fileURL name:name error:&error]; + for (int i = 0; i < [filePaths count]; i++) { + NSString *filePath = (NSString *) [filePaths objectAtIndex:i]; + NSString *uploadName = (NSString *) [names objectAtIndex:i]; + NSURL *fileURL = [NSURL URLWithString: filePath]; + [formData appendPartWithFileURL:fileURL name:uploadName error:&error]; + } if (error) { NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; [dictionary setObject:[NSNumber numberWithInt:500] forKey:@"status"];