当前位置: 动力学知识库 > 问答 > 编程问答 >

ios - Uploading multiple images from Xcode to php server

问题描述:

I want to upload multiple images to php server from my Xcode, when I am sending 1 image it is working well and for sending more than 1 image, the php is storing the only last image file. Please help me out of the issue. Below is my IOS code

 NSMutableURLRequest*request=[[NSMutableURLRequest alloc]init];

[request setURL:url];

[request setCachePolicy:NSURLRequestReloadIgnoringCacheData];

[request setHTTPMethod:@"POST"];

[request setHTTPShouldHandleCookies:NO];

[request setTimeoutInterval:30];

NSString *BoundaryConstant = @"----------V2ymHFg03ehbqgZCaKO6jy";

NSString*contentType=[NSString stringWithFormat:@"multipart/form-data; boundary=%@", BoundaryConstant];

[request addValue:contentType forHTTPHeaderField:@"Content-Type"];

NSMutableData*d=[NSMutableData data];

NSMutableDictionary*parameters=[[NSMutableDictionary alloc]init];

NSString*iD=[[NSUserDefaults standardUserDefaults]objectForKey:@"id"];

NSString*fiD=[[NSUserDefaults standardUserDefaults]objectForKey:@"familyid"];

NSString*eMailD=[[NSUserDefaults standardUserDefaults]objectForKey:@"email"];

[parameters setObject:iD forKey:@"user_id"];

[parameters setObject:self.modal.topicId forKey:@"topic_id"];

[parameters setObject:self.posttextView.text forKey:@"message"];

[parameters setObject:ddd forKey:@"posted_timestamp"];

[parameters setObject:fiD forKey:@"family_id"];

[parameters setObject:eMailD forKey:@"email_id"];

[parameters setObject:fmem forKey:@"member_id"];

NSString* FileParamConstant = @"filetoupload";

NSArray*filetoupload=[[NSArray alloc]initWithObjects:FileParamConstant, nil];

filetoupload=nil;

for (NSString *param in parameters) {

[d appendData:[[NSString stringWithFormat:@"--%@\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];

[d appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];

[d appendData:[[NSString stringWithFormat:@"%@\r\n", [parameters objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];

}

NSLog(@"the chosen images count is %lu",(unsigned long)self.chosenImagesDataobjects.count);

if (self.chosenImagesDataobjects.count>0) {

for (int s=0;s<self.chosenImagesDataobjects.count ; s++) {

[d appendData:[[NSString stringWithFormat:@"--%@\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];

[d appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image%d.jpg\"\r\n", FileParamConstant,s] dataUsingEncoding:NSUTF8StringEncoding]];

[d appendData:[[NSString stringWithFormat:@"Content-Type: image/jpeg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

[d appendData:[self.chosenImagesDataobjects objectAtIndex:s]];

[d appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

[d appendData:[[NSString stringWithFormat:@"--%@--\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];

}

}

[request setHTTPBody:d];

NSLog(@"the body of the total payload is %@",d);

NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[d length]];

[request setValue:postLength forHTTPHeaderField:@"Content-Length"];

NSOperationQueue*eighthQueue=[[NSOperationQueue alloc]init];

[NSURLConnection sendAsynchronousRequest:request queue:eighthQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

if ([data length]>0) {

NSLog(@"data greaterthan zero");

}

分享给朋友:
您可能感兴趣的文章:
随机阅读: