无法在请求正文中添加多个图像

时间:2014-03-13 10:48:37

标签: ios iphone nsurlrequest

我正在尝试在服务器上上传多个图像。为此,我在身体中添加了两个图像,但是当我记录身体时,它只显示一个图像

这是我的正文代码

NSMutableData *body = [NSMutableData data];

 NSString *boundary = [NSString stringWithFormat:@"AaB03x"];

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData1]];
[body appendData:[[NSString stringWithFormat:@"\r\n"]  dataUsingEncoding:NSUTF8StringEncoding]];//MOD HERE
//and add

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"userfile2\"; filename=\"ipodfile2.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData2]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

以下是记录正文的代码

 NSString* myString;
    myString = [[NSString alloc] initWithData:body encoding:NSASCIIStringEncoding];
    NSLog(@"body : %@",myString);

我得到了这个日志值

body : 
--AaB03x
Content-Disposition: form-data; name="userfile"; filename="ipodfile.jpg"
Content-Type: application/octet-stream

ÿØÿà

日志只显示一个文件而不是两个。

2 个答案:

答案 0 :(得分:3)

对所有图片使用以下部件名称

[body appendData:[@"Content-Disposition: form-data; name=\"images[]\"; filename=\"ipodfile1.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
...
[body appendData:[@"Content-Disposition: form-data; name=\"images[]\"; filename=\"ipodfile2.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

请注意name属性中的 images []


<强>更新

NSMutableData *multipartData = [NSMutableData data];

[multipartData appendData:[[NSString stringWithFormat:@"--%@\r\n", MULTIPART_REQUEST_BOUNDARY] dataUsingEncoding:NSUTF8StringEncoding]];
[multipartData appendData:[@"Content-Disposition: form-data; name=\"images[]\"; filename=\"test1.png\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[multipartData appendData:[@"Content-Type: image/png\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[multipartData appendData:imageData];
[multipartData appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

答案 1 :(得分:0)

试试这个

-(void)uploadMultiplePics  
{   
NSString *string ;   
NSData *imageData;   
NSString*urlString=[NSString stringWithFormat:@"http://******"];   
// urlString=[urlString stringByReplacingOccurrencesOfString:@" " withString:@""];   
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];   
[request setURL:[NSURL URLWithString:urlString]];    
[request setHTTPMethod:@"POST"];   
NSMutableData *body;   
body = [NSMutableData data];  
for(int j=0;j < scrollViewImageArray.count;j++)   // scrollViewImageArray is images count    
{      
double my_time = [[NSDate date] timeIntervalSince1970];   
int k=j+1;    
NSString *imageName = [NSString stringWithFormat:@"%d%d",j,(int)(my_time)];   
NSString *imagetag=[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"image%d\"; filename=\"",k];   
string = [NSString stringWithFormat:@"%@%@%@", imagetag, imageName, @".jpg\"\r\n\""];  
NSString *boundary = @"---------------------------14737809831466499882746641449";   
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];   
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];    
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];   
[body appendData:[[NSString stringWithString:string] dataUsingEncoding:NSUTF8StringEncoding]];   
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];   
UIImage *image=[[UIImage alloc]init];   
image=[scrollViewImageArray objectAtIndex:j];   
//  scrollViewImageArray images array   
imageData = UIImageJPEGRepresentation(image, 90);      
[body appendData:[NSData dataWithData:imageData]];   
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];     
}   
[request setHTTPBody:body];   
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];   
NSString*s=   [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];    
}

PHP代码供参考

$uploaddir = "../images/profile_images/";  
$file = basename($_FILES['image']['name']);  
$uploadfile = $uploaddir . $file;   
if (move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile))  
{  
$updt_img = "UPDATE tiny_users SET profile_photo = '".$file."' WHERE user_id = '".$final_res[1]."'";  
mysql_query($updt_img);  
}