我很清楚,您可能会将这个线程视为重复,因为这个问题以不同的方式被问了多次。我没有从这个论坛上现有的问题中找到任何解决方案,因此我将制作一个新的线程。
在这里,我有一个代码,可以一次上传多个图像。所以,请记下"多张图片上传"这句话。
我想上传1、2、3或4张图像以及一些文本参数,而我面临的问题是"同一张图像上传多次",即单个图像在服务器上有多次。
示例:
NSString *urlString = [NSString stringWithFormat:@"%@/MultipleImageUpload",soapAction];
NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc] init];
[theRequest setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[theRequest setHTTPShouldHandleCookies:NO];
[theRequest setTimeoutInterval:60];
[theRequest setURL:[NSURL URLWithString:urlString]];
[theRequest setHTTPMethod:@"POST"];
NSMutableData *body = [NSMutableData data];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",BOUNDARY_MULTIPART_IMAGE];
[theRequest addValue:contentType forHTTPHeaderField: @"Content-Type"];
//parameter SecurityCode
[body appendData:[[NSString stringWithFormat:@"--%@rn", BOUNDARY_MULTIPART_IMAGE] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="SecurityCode"rnrn"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[SEC_CODE dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"rn" dataUsingEncoding:NSUTF8StringEncoding]];
//parameter LoggedInUserID
[body appendData:[[NSString stringWithFormat:@"--%@rn", BOUNDARY_MULTIPART_IMAGE] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="LoggedInUserID"rnrn"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@“23433” dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"rn" dataUsingEncoding:NSUTF8StringEncoding]];
//parameter Device
[body appendData:[[NSString stringWithFormat:@"--%@rn", BOUNDARY_MULTIPART_IMAGE] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="Device"rnrn"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@“iPhone” dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"rn" dataUsingEncoding:NSUTF8StringEncoding]];
for (int i=0; i<[arrImgs count]; i++) {
UIImage* image = [arrImgs objectAtIndex:i];
NSData *imgData = UIImageJPEGRepresentation(image, 1.0);
[body appendData:[[NSString stringWithFormat:@"--%@rn", BOUNDARY_MULTIPART_IMAGE] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="product_photo"; filename="%d.jpg"rn", i] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/jpegrnrn" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imgData]];
[body appendData:[@"rn" dataUsingEncoding:NSUTF8StringEncoding]];
}
// close form
[body appendData:[[NSString stringWithFormat:@"--%@--rn", BOUNDARY_MULTIPART_IMAGE] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the request
[theRequest setHTTPBody:body];
return theRequest;
如果有人能解决这个问题,请告诉我。。
谢谢,
Nilesh M.Prajapati
对不起大家,但我找到了解决办法。实际上,我必须在下面几行中设置动态文件名。。我必须将"product_photo"设置为具有随机数的动态字符串,如"product_pphoto1"或"product_pPhoto2"等。
for (int i=0; i<[arrImgs count]; i++)
{
UIImage* image = [arrImgs objectAtIndex:i];
NSData *imgData = UIImagePNGRepresentation(image);
[body appendData:[[NSString stringWithFormat:@"--%@rn", BOUNDARY_MULTIPART_IMAGE] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="product_photo%d"; filename="%d.png"rn", i,i] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/pngrnrn" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imgData];
[body appendData:[@"rn" dataUsingEncoding:NSUTF8StringEncoding]];
}