Uploading an Array of images using AFNetworking

234 views Asked by At

I am trying to upload an array of images using AFNetwoking. The Web service requires these kind of parameters:

action: customer create album
deviceID: jsdkgf786
AlbumName: Drive testing
UserType: Customer
UserID: 1
EventId: 2
Privacy: 1
$_FILES[‘avtar’] array of files.

I am using this code to upload my files. But somehow the response is coming fine without any errors and the files are not getting uploaded as "null" is coming when I am fetching the images.

The code is as follows.

-(void)creatNewAlbum:(NSMutableArray *)arr_images withNames:(NSMutableArray *)arr_imageNames{
    // some extra work: will move it to different method
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"ddMMMyyyy_hhmmss"];


    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:serverUrl]];
   //[NSString stringWithFormat:@"AlbumName_%@",[formatter stringFromDate:[NSDate date]]]
    NSDictionary * dicParamsToSend  = @{
                                        @"action" : customerCreateAlbum,
                                        @"UserType" : [[UserBaseClass sharedApi].user objectForKey:@"UserType"],
                                        @"UserID": [[UserBaseClass sharedApi].user objectForKey:@"UserID"],
                                        @"deviceID": [AppDelegate sharedInstance].str_deviceUID,
                                        @"Privacy": [NSString stringWithFormat:@"%ld",(long)self.int_albumType],
                                        @"EventId": @2,
                                        @"AlbumName": self.str_albumName

                                        };
    [SVProgressHUD show];
    AFHTTPRequestOperation *op = [manager POST:@"rest.of.url" parameters:dicParamsToSend constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        //do not put image inside parameters dictionary as I did, but append it!
        int i = 0;
        for(UIImage *eachImage in arr_images)
        {
            NSData *imageData = UIImageJPEGRepresentation(eachImage,1);
            [formData appendPartWithFileData:imageData name:[NSString stringWithFormat:@"avtar[%d]",i ] fileName:[NSString stringWithFormat:@"%@",[arr_imageNames objectAtIndex:i]] mimeType:@"image/jpeg"];
            i++;
        }
        NSLog(@"the data to be sent is %@", formData);
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {

        NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);
        if ([[responseObject valueForKey:@"status"] isEqualToString:@"success"]) {
            UIAlertView * alert_success = [[UIAlertView alloc] initWithTitle:@"Success" message:[responseObject valueForKey:@"message"] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert_success show];
            [self firstLoad];
            [SVProgressHUD dismiss];
        }

        else{
            UIAlertView * alert_success = [[UIAlertView alloc] initWithTitle:@"Error!" message:[responseObject valueForKey:@"message"] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert_success show];
            [SVProgressHUD dismiss];
        }
    }
    failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        NSLog(@"Error: %@ ***** %@", operation.responseString, error);
    }];


    [op setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {

        //[progressView setProgress: totalBytesWritten*1.0f / totalBytesExpectedToWrite animated: YES];
        NSLog(@"Sent %lld of %lld bytes and progress is %f", totalBytesWritten, totalBytesExpectedToWrite, totalBytesWritten*1.0f /  totalBytesExpectedToWrite);
        if(totalBytesWritten >= totalBytesExpectedToWrite)
        {
            //progressView.hidden = YES;
        }
    }];

    [op start];
}

Please do provide a solution as it has taken up a lot of time. Any help will be appreciated. Thanks in advance.

0

There are 0 answers