How do post Json and Image to server for ios? I know post Json Data & Image , but I don't know post Json and Image at the same time? Because I want to order, I need post user info and user image to server.
2条回答 默认 最新
- duanjian3338 2014-02-21 04:49关注
-(void)postMethod { NSString *urlString = [NSString stringWithFormat:@"http:********web-services/register_user.php?firstname=%@&lastname=%@&email=%@&password=%@&location=india&device=IPHONE",details.fname,details.lname,details.emailAddress,details.password]; UIImage *image=details.pic; NSData *imageData =UIImageJPEGRepresentation(image, 0.1); double my_time = [[NSDate date] timeIntervalSince1970]; NSString *imageName = [NSString stringWithFormat:@"%d",(int)(my_time)]; NSString *string = [NSString stringWithFormat:@"%@%@%@", @"Content-Disposition: form-data; name=\"profile_pic\"; filename=\"", imageName, @".jpg\" \""]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; [request setURL:[NSURL URLWithString:urlString]]; [request setHTTPMethod:@"POST"]; NSString *boundary = @"---------------------------14737809831466499882746641449"; NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; [request addValue:contentType forHTTPHeaderField: @"Content-Type"]; NSMutableData *body = [NSMutableData data]; [body appendData:[[NSString stringWithFormat:@" --%@ ",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithString:string] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[@"Content-Type: application/octet-stream " dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[NSData dataWithData:imageData]]; [body appendData:[[NSString stringWithFormat:@" --%@-- ",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [request setHTTPBody:body]; NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; NSString*s11= [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 pnpm 下载element-plus
- ¥15 解决编写PyDracula时遇到的问题
- ¥15 有没有人能解决下这个问题吗,本人不会编程
- ¥15 plotBAPC画图出错
- ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
- ¥15 Python卡尔曼滤波融合
- ¥20 iOS绕地区网络检测
- ¥15 python验证码滑块图像识别
- ¥15 根据背景及设计要求撰写设计报告
- ¥20 能提供一下思路或者代码吗