为啥图像没有使用 POST 保存在服务器中?
Posted
技术标签:
【中文标题】为啥图像没有使用 POST 保存在服务器中?【英文标题】:Why image is not saved in the server using POST?为什么图像没有使用 POST 保存在服务器中? 【发布时间】:2016-07-01 05:29:44 【问题描述】:我必须将图像保存到服务器,但图像没有保存,其他东西正在保存。我正在使用这段代码,但没有得到我想要的结果......我必须将图像保存到这个地址 http://dev-demo.info.bh-in-15.webhostbox.net/dv/nationalblack/ulpoad/post/imagename
- (IBAction)SaveAction:(id)sender
NSString *businessStr = BusinessField.text;
NSString *addStr = AddressField.text;
NSString *webStr = websiteField.text;
NSString *fbStr = facebookField.text;
NSString *twitStr = twitterField.text;
NSString *instaStr = instagramField.text;
NSString *tumbStr = tumblerField.text;
NSString *pintStr = pinterestField.text;
NSString *shortStr = shortDescView.text;
NSString *longStr = longDescView.text;
NSString *categoryStr = categoridddd.text;
NSString *subcategoryStr = subcategoryidddd.text;
NSString *countStr = CountryLabel.text;
NSString *stateStr = StateLabel.text;
NSString *cityStr = cityField.text;
NSString *NameString = personNameField.text;
NSString *emailString = emailField.text;
NSString *phoneString = phoneField.text;
[self randomStringWithLength:8];
[self randomStringImageWithLength:5];
NSMutableData *body = [NSMutableData data];
NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
[parameters setValue:businessStr forKey:@"title"];
[parameters setValue:cityStr forKey:@"city"];
[parameters setValue:stateStr forKey:@"state"];
[parameters setValue:countStr forKey:@"country"];
[parameters setValue:addStr forKey:@"address"];
[parameters setValue:userID forKey:@"memberId"];
[parameters setValue:strName forKey:@"UserName"];
[parameters setValue:@"0" forKey:@"isfeatured"];
[parameters setValue:categoryStr forKey:@"prod_name"];
[parameters setValue:subcategoryStr forKey:@"var_name"];
[parameters setValue:@"0" forKey:@"package_name"];
[parameters setValue:@"0" forKey:@"category"];
[parameters setValue:shortStr forKey:@"shortdesc"];
[parameters setValue:longStr forKey:@"longdesc"];
[parameters setValue:@"35" forKey:@"amount"];
[parameters setValue:@" " forKey:@"catname"];
[parameters setValue:NameString forKey:@"contactperson"];
[parameters setValue:phoneString forKey:@"business_phone"];
[parameters setValue:@" " forKey:@"tags"];
[parameters setValue:@"365" forKey:@"subs_name"];
[parameters setValue:@"35" forKey:@"subs_price"];
[parameters setValue:timeStampStr forKey:@"paydate"];
[parameters setValue:timeStampStr forKey:@"startdate"];
[parameters setValue:endDateStr forKey:@"enddate"];
[parameters setValue:randomString forKey:@"transactionid"];
[parameters setValue:@"ZWZ3QDEyMw==" forKey:@"api_key"];
[parameters setValue:@"123456" forKey:@"api_password"];
[parameters setValue:webStr forKey:@"web_address"];
[parameters setValue:fbStr forKey:@"fb_address"];
[parameters setValue:twitStr forKey:@"tw_address"];
[parameters setValue:instaStr forKey:@"inst_address"];
[parameters setValue:pintStr forKey:@"pt_address"];
[parameters setValue:tumbStr forKey:@"tb_address"];
NSString *boundary = @"------VohpleBoundary4QuqLuM1cE5lMwCy";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:30];
[request setHTTPMethod:@"POST"];
NSURL *baseUrl = [NSURL URLWithString:@"http://dev-demo.info.bh-in-15.webhostbox.net/dv/nationalblack/api/addbusiness"];
// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];
// add params (all params are strings)
for (NSString *param in parameters)
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", [parameters objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
NSData *imageData = UIImageJPEGRepresentation(image1, 1.0);
NSString *FileParamConstant =@"ImageFile";
if (imageData)
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", FileParamConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the request
[request setHTTPBody:body];
// set URL
[request setURL:baseUrl];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
NSString *test = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"response : %@",test);
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
if ([httpResponse statusCode] == 200)
NSLog(@"success");
];
请帮帮我。我从昨天开始尝试这个。感谢您的帮助
【问题讨论】:
先学习ios基础知识。然后继续前进 【参考方案1】:- (IBAction)SaveAction:(id)sender
NSString *strServicePath = [NSString stringWithFormat:@"%@",@"http://dev-demo.info.bh-in-15.webhostbox.net/dv/nationalblack/api/addbusiness"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:strServicePath]];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:60];
[request setHTTPMethod:@"POST"];
// NSString *boundary = @"unique-consistent-string";
NSString *boundary = [NSString stringWithFormat:@"Boundary-%@", [[NSUUID UUID] UUIDString]];
// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
[parameters setValue:businessStr forKey:@"title"];
[parameters setValue:cityStr forKey:@"city"];
[parameters setValue:stateStr forKey:@"state"];
[parameters setValue:countStr forKey:@"country"];
[parameters setValue:addStr forKey:@"address"];
[parameters setValue:userID forKey:@"memberId"];
[parameters setValue:strName forKey:@"UserName"];
[parameters setValue:@"0" forKey:@"isfeatured"];
[parameters setValue:categoryStr forKey:@"prod_name"];
[parameters setValue:subcategoryStr forKey:@"var_name"];
[parameters setValue:@"0" forKey:@"package_name"];
[parameters setValue:@"0" forKey:@"category"];
[parameters setValue:shortStr forKey:@"shortdesc"];
[parameters setValue:longStr forKey:@"longdesc"];
[parameters setValue:@"35" forKey:@"amount"];
[parameters setValue:@" " forKey:@"catname"];
[parameters setValue:NameString forKey:@"contactperson"];
[parameters setValue:phoneString forKey:@"business_phone"];
[parameters setValue:@" " forKey:@"tags"];
[parameters setValue:@"365" forKey:@"subs_name"];
[parameters setValue:@"35" forKey:@"subs_price"];
[parameters setValue:timeStampStr forKey:@"paydate"];
[parameters setValue:timeStampStr forKey:@"startdate"];
[parameters setValue:endDateStr forKey:@"enddate"];
[parameters setValue:randomString forKey:@"transactionid"];
[parameters setValue:@"ZWZ3QDEyMw==" forKey:@"api_key"];
[parameters setValue:@"123456" forKey:@"api_password"];
[parameters setValue:webStr forKey:@"web_address"];
[parameters setValue:fbStr forKey:@"fb_address"];
[parameters setValue:twitStr forKey:@"tw_address"];
[parameters setValue:instaStr forKey:@"inst_address"];
[parameters setValue:pintStr forKey:@"pt_address"];
[parameters setValue:tumbStr forKey:@"tb_address"];
// post body
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
for (NSString *param in parameters)
NSMutableString *strPart = [NSMutableString stringWithFormat:@"%@",[self createPartWithKey:param value:[_params objectForKey:param] boundary:boundary encoding:@"8bit" contentType:@"text/plain" contentFilename:@""]];
[body appendData:[strPart dataUsingEncoding:NSUTF8StringEncoding]];
NSString *strProfileImageName = [NSString stringWithFormat:@"%@_.jpg",@"ImageFile"];
if (imageData)
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=%@; filename=%@\r\n", @"profile_pic",strProfileImageName] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// set the content-length
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[body length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
if(data != nil)
[HUD hideAnimated:YES];
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
NSInteger code = [httpResponse statusCode];
NSLog(@"Status Code: %ld", (long)code);
if (code == 200)
NSError *error;
id responseObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
);
【讨论】:
以上是关于为啥图像没有使用 POST 保存在服务器中?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 ASP.NET MVC 的服务器文件夹中保存 blob 图像
当我通过 POST 发送大于 20MB 的视频文件时,为啥 json 响应为 NULL?