2012-01-02 152 views
-2

在我的应用程序中,用户应该能够在服务器上传8张图片? 我可以在服务器上一次上传一张图片,但是如何一次或一次上传服务器上的8张图片。 请帮我出:如何在iphone上一次或一次上传8张不同的图片?

下面是我的代码:

-(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.com/upload/uploader.php?"]]; 
    [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:[arrImages objectAtIndex:0] withFileName:@"image.jpg" andContentType:@"image/jpeg" forKey:[NSString stringWithFormat:@"image%d", i + 1]] 
    [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

发送多个请求上传图像 – Ron 2012-01-02 06:51:59

+0

u能请编辑上面的代码,这样我可以得到乌尔点......谢谢 – 2012-01-02 07:05:19

+0

有一个数以千计计算器上解释这个代码。 – vikingosegundo 2012-01-02 12:17:38

回答

0

伪代码

int i = 0 
for (i<8) 
{ 
    //upload image no i here (define this before the for) 
    i++; 
} 

你可以做这一点,或使用数组 - 无论适合。

+1

这解释了什么?如何在伪代码中编写for循环? – vikingosegundo 2012-01-02 12:14:51

+0

可以请你帮我设置上面代码的多个帖子请求........谢谢。 – 2012-01-03 13:42:05