我正在做一个小项目,我需要将图像文件发送到服务器,因为我正在使用多部分请求。
以下是我的代码
UIImage *resizedImage1 = [img resizedImage:CGSizeMake(90.0f, 90.0f) interpolationQuality:kCGInterpolationHigh];
NSData *imageData = [NSData dataWithData:UIImageJPEGRepresentation(resizedImage1,0.8)];
NSString *urlString = @"myUrl/accounts/add_profile_image";
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];
// NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; image/jpeg; boundary=%@",boundary];
NSString *accessToken=[[NSUserDefaults standardUserDefaults]objectForKey:@"userAccesstoken"];
NSLog(@"access token %@",accessToken);
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
[request addValue:accessToken forHTTPHeaderField:@"auth"];
NSLog(@"request has %@",request);
// NSLog(@"imagedata has %@",imageData);
NSMutableData *postbody = [NSMutableData data];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// parameter v=1000
[postbody appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"v\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"1000\r\n"]dataUsingEncoding:NSUTF8StringEncoding]];
// mz_access_token sewt token parameter
[postbody appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"mz_access_token\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"%@\r\n",accessToken]dataUsingEncoding:NSUTF8StringEncoding]];
// image data parameter
[postbody appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"user_image\"; filename=\"image.jpg\"\r\n\r\n"]] dataUsingEncoding:NSUTF8StringEncoding]];
//[postbody appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
**[postbody appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];**
[postbody appendData:[NSData dataWithData:imageData]];
[postbody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"body has %@",postbody);
[request setHTTPBody:postbody];
// set the content-length
NSString *postLength = [NSString stringWithFormat:@"%d", [postbody length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"%@", returnString);
NSError *errorReturned = nil;
NSError *error = nil;
NSURLResponse *theResponse =[[NSURLResponse alloc]init];
//check netwrok
NSData *data = [NSURLConnection sendSynchronousRequest:request
returningResponse:&theResponse
error:&errorReturned];
if(data)
{
//parsing json
// again converting into ns dictionary object
NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: &error];
//NSLog(@"post data %@",postbody);
// NSLog(@"response of web ser %@ :\n %@",name,jsonArray);
NSLog(@"Response data %@",jsonArray);
// return jsonArray;
}else{
NSLog(@"error %@",error);
}
// return [[NSDictionary alloc]init];
});
这是我的网络服务请求。 我正在设置类似
的内容类型 [postbody appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
但仍然在服务器端我正在 @ content-type = nill
* 在服务器端,我们使用ROR作为webservice
*
请帮我解决这个问题。 提前谢谢..
答案 0 :(得分:0)
您应该按标题参数设置内容类型
[request setValue:@"image/jpeg" forHTTPHeaderField:@"Content-Type"];
请进行更改。
答案 1 :(得分:0)
对于每个multipart / form元素,您需要设置一个合适的Content-Type
,例如以下是文本参数:
[postbody appendData:[[NSString stringWithFormat:@"Content-Type: text/plain;charset=utf-8"] dataUsingEncoding:NSUTF8StringEncoding]];