iOS 将图像和数据发布到 PHP mySQL 数据库 [英] iOS Posting images and data to PHP mySQL Database
本文介绍了iOS 将图像和数据发布到 PHP mySQL 数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将多张图片和文本数据发布到我的 PHP 页面,然后发布到 mySQL 数据库中,但是我收到了 400 错误响应,我无法发布任何内容.
I am attempting to POST several pictures along with text data to my PHP page and then into mySQL database but I am getting a 400 error response and i am unable to post anything.
我只能发布数据或图片,但不能同时发布.
看来我没有正确设置POST.
I am able to POST only data or only pictures, but not both together.
It seems I am not setting up the POST correctly.
我需要发布很多图片,但现在只是想发布一张.
任何帮助将不胜感激!
NSData *imageData = UIImageJPEGRepresentation(_image1.image, 10);
//Add boundary
NSMutableString *boundary = [NSMutableString stringWithString:@"----Boundary+"];
//Append 5 random chars to the end of the boundary
for(int i = 0; i < 5; i++){
BOOL lowercase = arc4random() % 2;
if(lowercase){
[boundary appendFormat:@"%c", (arc4random() % 26) + 97];
}
else {
[boundary appendFormat:@"%c", (arc4random() % 26) + 65];
}
//Commit to mySQL Database
NSString *post = [NSString stringWithFormat:@"title=%@&price=%@&description=%@&latitude=%@&longitude=%@&location=%@&category_id=%@",sellTitle, sellPrice, sellDescription, sellLatitude, sellLongitude, sellLocation, sellCategory];
NSLog(@"PostData: %@",post);
NSURL *url=[NSURL URLWithString:@"http://myURL/testphp.php"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
//PICTURE STUFF
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 stringWithFormat:@"Content-Disposition: form-data; name=\"image\"; filename=\"picture.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"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]];
//Setting the body of the post to the request
[request setHTTPBody:body];
NSError *error = [[NSError alloc] init];
NSHTTPURLResponse *response = nil;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(@"Response code:%d", [response statusCode]);
if ([response statusCode] >=200 && [response statusCode] <300) {
NSString *responseData = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"Respinse ==> %@", responseData);
//Now lets make the connection to the web
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"%@",returnString);
NSLog(@"%@",boundary);
NSLog(@"Title Saved: %@", sellTitle);
推荐答案
这是我设法为此工作的代码.这是完美的工作.
Here is the code i managed to get working for this. This is working perfectly.
//begin new method
NSString *urlString = @"http://www.yourURLforPostingData.com";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[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"];
// picture
NSData *imageData = UIImageJPEGRepresentation(_image1.image, .25);
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"image\"; filename=\"picture.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"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]];
// Description Text
NSString *description = [defaults objectForKey:@"description"];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"description\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:description] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// Title Text
NSString *title = [defaults objectForKey:@"sellTitle"];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"title\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:title] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// close form
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// set request body
[request setHTTPBody:body];
//return and test
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"%@", returnString);
//END NEW METHOD
这篇关于iOS 将图像和数据发布到 PHP mySQL 数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文