使用HTTP POST上传多个图像

时间:2014-04-24 10:23:14

标签: objective-c ios7 http-post

我正在尝试使用HTPP POST方法上传多个图像。但不知何故它不起作用。我每次上传3张图片,但只上传了1张图片。

以下是代码:

- (void)sendImageUploadRequest:(NSMutableArray *)arrImage
{
    if(arrImage)
    {
        NSString *strUrl = [NSString stringWithFormat:@"upload.php"];
        NSString *urlString = [NSString stringWithFormat:ServerURL,strUrl];

        NSString *method =  @"POST";
        NSString *email   = @"xyz@gmail.com";
//        NSString *token   = @"";

        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
        [request setURL:[NSURL URLWithString:urlString]];
        [request setHTTPMethod:@"POST"];

        NSMutableData *body = [NSMutableData data];

        NSString *boundary;
            boundary = [NSString stringWithFormat:@"---------------------------14737809831466499882746641449"];

        // file
        for (int i = 0; i < [arrImagesUpload count]; i++) {

            NSData *imageData = UIImageJPEGRepresentation([arrImagesUpload objectAtIndex:i], 0.8);
            NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
            [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

            [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
            NSString *strNameImage = [NSString stringWithFormat:@"test%d.jpg", i];
            NSString *str = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"images[]\"; filename=\"%@\"\r\n", strNameImage];
            [body appendData:[str dataUsingEncoding:NSUTF8StringEncoding]];
            [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
            [body appendData:[NSData dataWithData:imageData]];
            [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
            [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];    
        }

            //  parameter username
            [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"email\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
            [body appendData:[email dataUsingEncoding:NSUTF8StringEncoding]];
            [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

            //    //  parameter method
            [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
            [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"method\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
            [body appendData:[method dataUsingEncoding:NSUTF8StringEncoding]];
            [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

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

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

        // now lets make the connection to the web
        NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
        NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

        NSLog(@"%@",returnString);
    }
}

任何人都可以帮我解决问题。

感谢。

1 个答案:

答案 0 :(得分:1)

经过一番努力终于让它成功了。以下是工作代码:

Image