@@ -460,62 +460,127 @@ - (void)uploadFiles:(CDVInvokedUrlCommand*)command {
460460 NSDictionary *headers = [command.arguments objectAtIndex: 1 ];
461461 NSArray *filePaths = [command.arguments objectAtIndex: 2 ];
462462 NSArray *names = [command.arguments objectAtIndex: 3 ];
463- NSTimeInterval connectTimeout = [[command.arguments objectAtIndex: 4 ] doubleValue ];
463+ NSTimeInterval _connectTimeout = [[command.arguments objectAtIndex: 4 ] doubleValue ];
464464 NSTimeInterval readTimeout = [[command.arguments objectAtIndex: 5 ] doubleValue ];
465465 bool followRedirect = [[command.arguments objectAtIndex: 6 ] boolValue ];
466466 NSString *responseType = [command.arguments objectAtIndex: 7 ];
467- NSNumber *reqId = [command.arguments objectAtIndex: 8 ];
468-
469- [self setRequestHeaders: headers forManager: manager];
470- [self setTimeout: readTimeout forManager: manager];
471- [self setRedirect: followRedirect forManager: manager];
472- [self setResponseSerializer: responseType forManager: manager];
467+ NSDictionary *transmitOptions = [command.arguments objectAtIndex: 8 ];
468+ NSNumber *reqId = [command.arguments objectAtIndex: 9 ];
469+
470+ NSString *transmitFileType = [transmitOptions objectForKey: @" transmitFileAs" ];
473471
474472 CordovaHttpPlugin* __weak weakSelf = self;
475473 [[SDNetworkActivityIndicator sharedActivityIndicator ] startActivity ];
476474
477- @try {
478- NSURLSessionDataTask *task = [manager POST: url parameters: nil constructingBodyWithBlock: ^(id <AFMultipartFormData> formData) {
479- NSError *error;
480- for (int i = 0 ; i < [filePaths count ]; i++) {
481- NSString *filePath = (NSString *) [filePaths objectAtIndex: i];
482- NSString *uploadName = (NSString *) [names objectAtIndex: i];
483- NSURL *fileURL = [NSURL URLWithString: filePath];
484- [formData appendPartWithFileURL: fileURL name: uploadName error: &error];
485- }
486- if (error) {
487- [weakSelf removeRequest: reqId];
488-
489- NSMutableDictionary *dictionary = [NSMutableDictionary dictionary ];
490- [dictionary setObject: [NSNumber numberWithInt: 500 ] forKey: @" status" ];
491- [dictionary setObject: @" Could not add file to post body." forKey: @" error" ];
492- CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus: CDVCommandStatus_ERROR messageAsDictionary: dictionary];
493- [weakSelf.commandDelegate sendPluginResult: pluginResult callbackId: command.callbackId];
494- [[SDNetworkActivityIndicator sharedActivityIndicator ] stopActivity ];
495- return ;
496- }
497- } progress: nil success: ^(NSURLSessionTask *task, id responseObject) {
475+ @try
476+ {
477+ NSURLSessionDataTask * task;
478+
479+ void (^setupManager)(void ) = ^() {
480+ [self setRequestHeaders: headers forManager: manager];
481+ [self setTimeout: readTimeout forManager: manager];
482+ [self setRedirect: followRedirect forManager: manager];
483+ [self setResponseSerializer: responseType forManager: manager];
484+ };
485+
486+ void (^onFailure)(NSURLSessionTask *, NSError *) = ^(NSURLSessionTask *task, NSError *error) {
498487 [weakSelf removeRequest: reqId];
499488
500489 NSMutableDictionary *dictionary = [NSMutableDictionary dictionary ];
501- [self handleSuccess : dictionary withResponse: (NSHTTPURLResponse *)task.response andData: responseObject ];
490+ [self handleError : dictionary withResponse: (NSHTTPURLResponse *)task.response error: error ];
502491
503- CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK messageAsDictionary: dictionary];
492+ CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus: CDVCommandStatus_ERROR messageAsDictionary: dictionary];
504493 [weakSelf.commandDelegate sendPluginResult: pluginResult callbackId: command.callbackId];
505494 [[SDNetworkActivityIndicator sharedActivityIndicator ] stopActivity ];
506- } failure: ^(NSURLSessionTask *task, NSError *error) {
495+ [manager invalidateSessionCancelingTasks: YES ];
496+ };
497+
498+ void (^onSuccess)(NSURLSessionTask *, id ) = ^(NSURLSessionTask *task, id responseObject) {
507499 [weakSelf removeRequest: reqId];
508500
509501 NSMutableDictionary *dictionary = [NSMutableDictionary dictionary ];
510- [self handleError : dictionary withResponse: (NSHTTPURLResponse *)task.response error: error ];
502+ [self handleSuccess : dictionary withResponse: (NSHTTPURLResponse *)task.response andData: responseObject ];
511503
512- CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus: CDVCommandStatus_ERROR messageAsDictionary: dictionary];
504+ CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK messageAsDictionary: dictionary];
513505 [weakSelf.commandDelegate sendPluginResult: pluginResult callbackId: command.callbackId];
514506 [[SDNetworkActivityIndicator sharedActivityIndicator ] stopActivity ];
515- }];
507+ [manager invalidateSessionCancelingTasks: YES ];
508+ };
509+
510+ bool submitRaw = [transmitFileType isEqualToString: @" BINARY" ];
511+
512+ // RAW
513+ if (submitRaw)
514+ {
515+ if ([filePaths count ] == 1 )
516+ {
517+ // setup the request serializer to submit the raw file content
518+ manager.requestSerializer = [BinaryRequestSerializer serializer ];
519+ setupManager ();
520+
521+ NSURL *fileURL = [NSURL URLWithString: [filePaths objectAtIndex: 0 ]];
522+ NSError *error;
523+ NSData *fileData = [NSData dataWithContentsOfURL: fileURL options: 0 error: &error];
524+
525+ if (error)
526+ {
527+ NSMutableDictionary *dictionary = [NSMutableDictionary dictionary ];
528+ [self handleError: dictionary withResponse: nil error: error];
529+ CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus: CDVCommandStatus_ERROR messageAsDictionary: dictionary];
530+ [weakSelf.commandDelegate sendPluginResult: pluginResult callbackId: command.callbackId];
531+ [[SDNetworkActivityIndicator sharedActivityIndicator ] stopActivity ];
532+ [manager invalidateSessionCancelingTasks: YES ];
533+ return ;
534+ }
535+
536+ task = [manager uploadTaskWithHTTPMethod: @" POST" URLString: url parameters: fileData progress: nil success: onSuccess failure: onFailure];
537+ }
538+ else
539+ {
540+ [NSException raise: @" ArgumentException" format: @" Can only transmit a single file. Multiple files are not supported in this mode." ];
541+ }
542+ }
543+ else // FALLBACK: Multipart / FormData
544+ {
545+ setupManager ();
546+ task = [manager
547+ POST: url
548+ parameters: nil
549+ constructingBodyWithBlock: ^(id <AFMultipartFormData> formData)
550+ {
551+ NSError *error;
552+
553+ for (int i = 0 ; i < [filePaths count ]; i++)
554+ {
555+ NSString *filePath = (NSString *) [filePaths objectAtIndex: i];
556+ NSString *uploadName = (NSString *) [names objectAtIndex: i];
557+ NSURL *fileURL = [NSURL URLWithString: filePath];
558+ [formData appendPartWithFileURL: fileURL name: uploadName error: &error];
559+ }
560+
561+ if (error)
562+ {
563+ [weakSelf removeRequest: reqId];
564+
565+ NSMutableDictionary *dictionary = [NSMutableDictionary dictionary ];
566+ [dictionary setObject: [NSNumber numberWithInt: 500 ] forKey: @" status" ];
567+ [dictionary setObject: @" Could not add file to post body." forKey: @" error" ];
568+ CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus: CDVCommandStatus_ERROR messageAsDictionary: dictionary];
569+ [weakSelf.commandDelegate sendPluginResult: pluginResult callbackId: command.callbackId];
570+ [[SDNetworkActivityIndicator sharedActivityIndicator ] stopActivity ];
571+ return ;
572+ }
573+ }
574+ progress: nil
575+ success: onSuccess
576+ failure: onFailure
577+ ];
578+ }
579+
516580 [self addRequest: reqId forTask: task];
517581 }
518- @catch (NSException *exception) {
582+ @catch (NSException *exception)
583+ {
519584 [[SDNetworkActivityIndicator sharedActivityIndicator ] stopActivity ];
520585 [self handleException: exception withCommand: command];
521586 }
0 commit comments