-1

我有一个例子,我将图像和参数发送到服务器。但我需要发送图像的 NSArray。我该怎么做?

 NSData *imageToUpload = UIImageJPEGRepresentation(uploadedImgView.image, 1.0);//(uploadedImgView.image);
if (imageToUpload)
{
NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:keyParameter, @"keyName", nil];

AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://------"]];

NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:@"API name as you have" parameters:parameters constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
    [formData appendPartWithFileData: imageToUpload name:@"image" fileName:@"temp.jpeg" mimeType:@"image/jpeg"];
}];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
 {
     NSDictionary *jsons = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
     //NSLog(@"response: %@",jsons);

 }
                                 failure:^(AFHTTPRequestOperation *operation, NSError *error)
 {
     if([operation.response statusCode] == 403)
     {
         //NSLog(@"Upload Failed");
         return;
     }
     //NSLog(@"error: %@", [operation error]);

 }];

[operation start];
}

请帮帮我!

4

2 回答 2

4

我找到了答案,我将代码更改为:

[formData appendPartWithFileData:data name:@"Photos[1]" fileName:@"avatar.jpg" mimeType:@"image/jpeg"];
[formData appendPartWithFileData:data name:@"Photos[2]" fileName:@"avatar.jpg" mimeType:@"image/jpeg"];

和服务器将其理解为数组

于 2013-08-13T08:28:25.207 回答
0

你可以试试这个

NSURL *url = [NSURL URLWithString:@URL_BASE];
int count = [imageArray count];

for(int i = 0; i < count; ++i)
 {
     UIImage* image = [imageArray objectAtIndex:i];
     AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
     NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:
                                             [NSNumber numberWithFloat: image.size.width], @"width",
                                             [NSNumber numberWithFloat: image.size.height], @"height",
                                          nil];

     NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@PATH_ALBUM_IMAGE_UPLOAD parameters:params constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
             [formData appendPartWithFileData:imageData name:[NSString stringWithFormat:@"file%d", i]  fileName:@"avatar.jpg" mimeyTpe:@"image/jpeg"];
         }];

     AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
     [operation setUploadProgressBlock:^(NSInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {

             [progressView setProgress: totalBytesWritten*1.0f / totalBytesExpectedToWrite animated: YES];
             NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
             if(totalBytesWritten >= totalBytesExpectedToWrite)
             {
                 progressView.hidden = YES;
             }
         }];
     [operation start];
 }
于 2013-08-07T07:45:31.687 回答