简体   繁体   English

如何将一幅以上的图像发送到服务器

[英]How to send more then one image to server

I'm able to send single image to the server. 我可以将单个图像发送到服务器。 Now I need to modify the code to send two images in two different urls . 现在,我需要修改代码以使用two different urls发送two images The code i've used to send singe image is 我用来发送单张图片的代码是

NSString *url=[NSString stringWithFormat:@"http://37.187.152.236/UserImage.svc/InsertObjectImage?%@",requestString];
NSLog(@"url1%@",url);
 NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];

// Create 'POST' MutableRequest with Data and Other Image Attachment.

NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",   boundary];
[request setValue:contentType forHTTPHeaderField:@"Content-Type"];

 NSData *data = UIImageJPEGRepresentation(chosenImage1, 0.2f);
 [request addValue:@"image/JPEG" forHTTPHeaderField:@"Content-Type"];
 NSMutableData *body = [NSMutableData data];
 [body appendData:[NSData dataWithData:data]];
 [request setHTTPBody:body];

Help me, Thanks in advance for everyone. 帮帮我,谢谢大家。

Try Following Code Make Sure You assign Images and Urls properly 尝试按照以下代码进行操作确保您正确分配了图像和Urls

UIImage * image1 ;
UIImage * image2;

NSString * imageUrl1;
NSString * imageUrl2;



NSMutableArray * arrImageData=[[NSMutableArray alloc]initWithObjects:image1,image2,nil];

NSMutableArray * arrImageUrls=[[NSMutableArray alloc]initWithObjects:imageUrl1,imageUrl2,nil];

for(int i=0; i < arrImageData.count ; i++){


    NSString *url=[NSString stringWithFormat:@"http://37.187.152.236/UserImage.svc/InsertObjectImage?%@",[arrImageUrls objectAtIndex:i]];
    NSLog(@"url1%@",url);
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
    [request setURL:[NSURL URLWithString:url]];
    [request setHTTPMethod:@"POST"];

    // Create 'POST' MutableRequest with Data and Other Image Attachment.

    NSString *boundary = @"---------------------------14737809831466499882746641449";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",   boundary];
    [request setValue:contentType forHTTPHeaderField:@"Content-Type"];

    UIImage * chosenImage1=[arrImageData objectAtIndex:i];

    NSData *data = UIImageJPEGRepresentation(chosenImage1, 0.2f);
    [request addValue:@"image/JPEG" forHTTPHeaderField:@"Content-Type"];
    NSMutableData *body = [NSMutableData data];
    [body appendData:[NSData dataWithData:data]];
    [request setHTTPBody:body];

}

For sending multiple images in the same url at a time to the server you have to use base64 conversion of images and then add all converted image's string into the JSON . 要一次将相同url中的多个图像发送到服务器,您必须使用图像的base64转换,然后将所有转换后的图像字符串添加到JSON After JSON formatting you can simply send those images to the server. JSON格式化后,您只需将这些图像发送到服务器即可。 Same in the server side you have to decode those base64 converted image's string to image data and save it to the directory. 同样,在服务器端,您必须将那些经过base64转换的图像的字符串解码为图像data ,并将其保存到目录中。 For base64 Encode and Decode please refer to this link 对于base64 EncodeDecode请参考此链接

For sending two images to two different url's you can proceed with Harish Kanojiya answer. 要将两个图像发送到两个不同的URL,可以继续使用Harish Kanojiya答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM