2013-02-13 47 views
0

我无法上传多个图像到服务器。我正在使用以下代码上传:使用ASIHTTPRequest将多个图像上传到服务器上使用ASIHTTPRequest

-(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]; 
} 

通过此代码,我只能上传一张图片。如何使用相同的密钥上传多个图像?

+0

@AppleDelegate编辑,使更适合于正确的缩进,大写字母和白色空间,对于那些看到它的人来说更加可行。 – rptwsthi 2013-02-13 07:08:25

+0

由于您的网络服务一次只能接受一张图片,因此您无法发送多个图片,请不断发送您的网络服务,直到您发送所有图片或对该加载图片数组进行一些修改或像image1,image2 ...等图像。 – rptwsthi 2013-02-13 07:10:57

+0

@rptwsthi我的代码上有任何错误... – IKKA 2013-02-13 07:19:06

回答

3

请求:ASIFormDataRequest是非常古老的,没有被作者更新。你应该切换到AFNetworking

如果您使用PHP作为后端以这种方式接收文件,您应该可以发送文件数组。

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]; 

诀窍是用图像[0],图像1,图像[2]等的键,用于将多个文件。尝试改变你的代码,它也应该与你当前的库一起工作。

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

在你的PHP你应该收到的文件这样的..

(
[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 
      ) 

    ) 

希望这能解决您的问题.. :-)

+0

我们班导入了哪一类AFNetworking – 2013-12-01 13:56:14

相关问题