0
我试图将几张图片和文本数据一起发送到我的PHP页面,然后进入mySQL数据库,但我得到了400错误响应,我无法发布任何内容。iOS将图像和数据发布到PHP mySQL数据库
我可以只发布数据或只发布图片,但不能同时发布。
看来我没有正确设置POST。
我需要发布很多图片,但只是试图让一个滚动现在。
任何帮助将不胜感激!
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);
没有接受任何人吗? – mreynol