Php Can';t使用POST方法在单个NSURL请求中上载2个图像
我们通过多次分离两个图像数据向API发送正确的post请求。因此,两个图像都没有上载,只有一个图像正在上载到服务器。我已经检查了API团队提供的参数名称,如下所示:-Php Can';t使用POST方法在单个NSURL请求中上载2个图像,php,ios,iphone,ipad,nsurlrequest,Php,Ios,Iphone,Ipad,Nsurlrequest,我们通过多次分离两个图像数据向API发送正确的post请求。因此,两个图像都没有上载,只有一个图像正在上载到服务器。我已经检查了API团队提供的参数名称,如下所示:- #define IMAGE_DP @"Content-Disposition: form-data; name=\"profileimage\"; filename=\"image.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n" #define IMAGE_COVER @"Content-D
#define IMAGE_DP @"Content-Disposition: form-data; name=\"profileimage\"; filename=\"image.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n"
#define IMAGE_COVER @"Content-Disposition: form-data; name=\"coverimage\"; filename=\"image.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n"
在一个NSURLTABLE请求中上传两个图像的生成请求
if ([isprofilepicupdated isEqualToString:@"1"]) {
[postBody appendData:[IMAGE_DP dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[NSData dataWithData:profileimage]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
}
if ([iscoverpicupdated isEqualToString:@"1"]) {
[postBody appendData:[IMAGE_COVER dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[NSData dataWithData:coverimage]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
}
那么我们如何在NSURLMutable请求的单个正文中附加两个图像呢?NSString*urlString=@”;
NSString *urlString = @"<PostImage-url>";
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSMutableData *body = [NSMutableData data];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
// file
for (int i=0;i<(YOUR _ARRAY).count;i++)
{
UIImage * myimg=[YOUR _ARRAY objectAtIndex:i];
NSData *ImageData = UIImagePNGRepresentation(myimg);
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: attachment; name=\"img1[]\"; filename=\".jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:ImageData]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"vin\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
// close form
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// set request body
[request setHTTPBody:body];
//ASynchronous request
[[NSURLConnection alloc] initWithRequest:request delegate:self];
NSMutableURLRequest*请求=[[[NSMutableURLRequest alloc]init]autorelease];
[请求设置URL:[NSURL URLWithString:urlString]];
[请求设置HttpMethod:@“POST”];
NSMutableData*正文=[NSMutableData];
NSString*boundary=@“------------------------------------14737809831466499882746641449”;
NSString*contentType=[NSString stringWithFormat:@“多部分/表单数据;边界=%@”,边界];
[请求addValue:contentType for HttpHeaderField:@“内容类型”];
//文件
对于(int i=0;i