我将图像上传到服务器时出现问题。我还需要使用该图像将字符串值上传到服务器。图片上传很好,但到目前为止我还没有成功上传字符串。以下是我上传的代码。请帮帮我。谢谢。
NSData *imageData = UIImageJPEGRepresentation(theImage,0.9);
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString* width =@"10";
[request setValue:@"300" forHTTPHeaderField:@"Keep-Alive"];
[request setValue:@"keep-live" forHTTPHeaderField:@"Connection"];
[request setValue:width forHTTPHeaderField:@"spotfk"];
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
Spots* spotObj=[spotsList objectAtIndex:spotIdValue-1];
SpotItems* spotItemObj=[[SpotItems alloc]init];
spotItemObj.imageX_Coordinate=0.0;
spotItemObj.imageY_Coordinate=0.0;
spotItemObj.imageWidth=spotObj.spotWidth;
spotItemObj.imageHight=spotObj.spotHight;
spotItemObj.imageName=@"logo.png";
UIView* subView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, spotObj.spotWidth, spotObj.spotHight)];
subView.backgroundColor=[UIColor clearColor];
spotItemObj.subView=subView;
UIImageView* newSpotItemImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, spotObj.spotWidth, spotObj.spotHight)];
newSpotItemImageView.image=[UIImage imageNamed:spotItemObj.imageName];
[spotItemObj.subView addSubview:newSpotItemImageView];
[spotObj.spotsItemsList addObject:spotItemObj];
[spotItemObj release];
if([spotObj.spotsItemsList count]==2)
{
[self startInternalAnimation:spotObj.spotId-1];
}
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
答案 0 :(得分:0)
我得到了解决方案,
那就是我们需要制作如下代码。问题是没有使用dataUsingEndcoding方法和边界,现在代码如下。
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"spotfk\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:spotFk] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"media\"; filename=\"test.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:imageData];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postBody];