0
votes

I am having trouble uploading more than one image to server. I am using following code for uploading:

-(void)upload {
 NSString *path=[NSString stringWithFormat:@"%@.php",webserviceURL];
 ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:path]];
 [request addPostValue:@"306" forKey:@"id"];

 for (int i= 0; i<[imageArray count]; i++) {
    NSData *imgData = UIImageJPEGRepresentation([imageArray objectAtIndex:i],1.0);
    NSDate *date....

     .....

    NSLog(@"dateString");//for getting different file name..

    [request addData:imgData withFileName:[NSString stringWithFormat:@"%@.jpg",dateString] 
    andContentType:@"image/jpeg" forKey:@"image"];
  }

 [request setDelegate:self]; 
 [request startAsynchronous];
}

With this code I am able to upload only one image. How can I upload multiple images using the same key?

1
@AppleDelegate edit it to make more appropriate with proper indentation, and capitalization along with white spaces, that will be more feasible for those who see it.rptwsthi
as your web service is made to accept one image at a time, you can't send more than one image, keep calling your web service back to back till you send all your image or make some modification in that lo load image array or images like image1, image2... and so on.rptwsthi
@rptwsthi any mistake on my code...IKKA
Yes! it'll send only last image as you are overwriting image to only key i.e. "image".rptwsthi
@rptwsthi yes you are right,then how to possible upload more than one image to server?IKKA

1 Answers

3
votes

Request: ASIFormDataRequest is very old and not being updated by the author. You should switch to AFNetworking.

If you are using PHP as your backend for receiving files this way you should be able to send array of files.

NSData *imageToUpload = UIImageJPEGRepresentation([UIImage imageNamed:@"profile-image-placeholder"], 10);
AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://www.imthi.com"]];

NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:@"/info.php" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
    [formData appendPartWithFileData: imageToUpload name:@"image[0]" fileName:@"temp.jpeg" mimeType:@"image/jpeg"];
    [formData appendPartWithFileData: imageToUpload name:@"image[1]" fileName:@"temp.jpeg" mimeType:@"image/jpeg"];
    [formData appendPartWithFileData: imageToUpload name:@"image[2]" fileName:@"temp.jpeg" mimeType:@"image/jpeg"];
}];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation * _operation, id responseObject) {
    NSString *response = [_operation responseString];
    NSLog(@"response: [%@]",response);
} failure:^(AFHTTPRequestOperation * _operation, NSError *error) {
    if([_operation.response statusCode] == 403){
        NSLog(@"Upload Failed");
        return;
    }
}];
[operation start];

The trick is to use image[0], image1, image[2] etc as the key for adding more files. Try changing in your code and it should work with your current library too.

[request addData:imgData withFileName:[NSString stringWithFormat:@"%@.jpg",dateString] andContentType:@"image/jpeg" forKey:[NSString stringWithFormat:@"image[%d]",i]];

On your PHP you should receive the files like this..

(
[image] => Array
    (
        [name] => Array
            (
                [0] => temp.jpeg
                [1] => temp.jpeg
                [2] => temp.jpeg
            )

        [type] => Array
            (
                [0] => image/jpeg
                [1] => image/jpeg
                [2] => image/jpeg
            )

        [tmp_name] => Array
            (
                [0] => /tmp/php4XsVR8
                [1] => /tmp/phpDBPzVO
                [2] => /tmp/phpu26eZu
            )

        [error] => Array
            (
                [0] => 0
                [1] => 0
                [2] => 0
            )

        [size] => Array
            (
                [0] => 2207
                [1] => 2207
                [2] => 2207
            )

    )

)

Hope this solves your issue.. :-)