Search code examples
iosobjective-cnsurlconnection

How to upload several images with httpPost?


I want to upload images using http post but my code causes a 500 error.

One file is ok, but two files or more files cause a 500 error.

Is it server problem? I don't know why.

Here is my upload code part:

-(void)sendImageByPost:(NSMutableArray *)images{
//create request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

//Set Params
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:60];
[request setHTTPMethod:@"POST"];

//Create boundary, it can be anything
NSString *boundary = @"------VohpleBoundary4QuqLuM1cE5lMwCy";

// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];

// post body
NSMutableData *body = [NSMutableData data];

NSString *FileParamConstant = @"imageParamName";

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSDate *now;
NSString *date;
[formatter setDateFormat:@"yyyyMMddhhmmss"];


[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

for(UIImage * image in images){
    NSData *imageData = UIImageJPEGRepresentation(image, 1);
    [NSThread sleepForTimeInterval:1];
    now = [NSDate date];
    date = [formatter stringFromDate:now];

    //Assuming data is not nil we add this to the multipart form
    if (imageData)
    {
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@.jpg\"\r\n", FileParamConstant, date] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:imageData];
    }
}


[body appendData:[@"Content-Type:image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
//Close off the request with the boundary
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

// setting the body of the post to the request
[request setHTTPBody:body];

// set URL
[request setURL:[NSURL URLWithString:@"http://52.68.115.148/api/fileupload"]];

[NSURLConnection sendAsynchronousRequest:request
                                   queue:[NSOperationQueue mainQueue]
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

                           NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;

                           if ([httpResponse statusCode] == 201) {

                               NSLog(@"success");
                           }
                           else{
                               NSLog(@"fail");
                               NSLog(@"%@", [NSString stringWithFormat:@"%d", [httpResponse statusCode]]);
                           }

                       }];
}

Solution

  • It seems that your request's body is not correct multipart/form-data.

    You forgot boundary after each image. Try to change

    if (imageData)
        {
            [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@.jpg\"\r\n", FileParamConstant, date] dataUsingEncoding:NSUTF8StringEncoding]];
            [body appendData:imageData];
        }
    

    to

    if (imageData)
        {
            [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@.jpg\"\r\n", FileParamConstant, date] dataUsingEncoding:NSUTF8StringEncoding]];
            [body appendData:imageData];
            [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    
        }