如何在iphone上的服务器上上传多个图像?

时间:2011-12-19 05:18:57

标签: ios iphone objective-c

在我的应用程序中,用户应该能够在服务器上上传多个图像(一次最多8张图像)? 我可以一次上传一张图片,但我一次无法上传多张图片? 请给我一些tutorail,这对我有帮助吗? 谢谢。

以下是我的代码:

-(IBAction)upload:(id)sender
{

NSDateFormatter *format = [[NSDateFormatter alloc] init]; [format setDateFormat:@"yyyyMMddHHmmss"];

    NSDate *now = [[NSDate alloc] init];

    NSString *imageName = [NSString stringWithFormat:@"Image_%@", [format stringFromDate:now]];

    [now release];
    [format release];

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:@"http://url"]];
    [request setHTTPMethod:@"POST"];

    /*
     Set Header and content type of your request.
     */
    NSString *boundary = [NSString stringWithString:@"---------------------------Boundary Line---------------------------"];
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

    /*
     now lets create the body of the request.
     */
    NSMutableData *body = [NSMutableData data];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];    
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.jpg\"\r\n", imageName] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:UIImageJPEGRepresentation(imageView.image, 90)]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    //[body appendData:[[NSString stringWithFormat:@"geotag=%@&", [self _currentLocationMetadata]] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    // set body with request.
    [request setHTTPBody:body];
    [request addValue:[NSString stringWithFormat:@"%d", [body length]] forHTTPHeaderField:@"Content-Length"];

    // now lets make the connection to the web
    [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    //


}

0 个答案:

没有答案