2013-12-11 00:33
采纳率: 0%
浏览 37

iOS将图像和数据发布到PHP mySQL数据库

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.

I am able to POST only data or only pictures, but not both together.
It seems I am not setting up the POST correctly.

I need to post many pictures but was just trying to get one rolling for now.

Any help would be greatly appreciated!

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;

        [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];

    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 stringWithFormat:@"Content-Disposition: form-data; name=\"image\"; filename=\"picture.png\"
"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type: application/octet-stream

" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData]];
    [body appendData:[[NSString stringWithFormat:@"
",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(@"Title Saved: %@", sellTitle);

1条回答 默认 最新

相关推荐 更多相似问题