dsgdg54ef4365 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;

    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:@"
--%@
",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(@"%@",returnString);
        NSLog(@"%@",boundary);
        NSLog(@"Title Saved: %@", sellTitle);
  • 写回答

1条回答 默认 最新

  • duanqianmou4661 2014-01-06 16:27
    关注

    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:@"
    --%@
    ",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]];
    
        // Description Text
        NSString *description = [defaults objectForKey:@"description"];
        [body appendData:[[NSString stringWithFormat:@"--%@
    ", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"description\"
    
    "] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithString:description] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[@"
    " dataUsingEncoding:NSUTF8StringEncoding]];
    
        // Title Text
        NSString *title = [defaults objectForKey:@"sellTitle"];
        [body appendData:[[NSString stringWithFormat:@"--%@
    ", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"title\"
    
    "] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithString:title] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[@"
    " dataUsingEncoding:NSUTF8StringEncoding]];
    
    
        // close form
        [body appendData:[[NSString stringWithFormat:@"--%@--
    ", 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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 为啥输入字体突然变了
  • ¥20 已知坐标,换成MATLAB可以用的数据
  • ¥50 写segy数据text时出错
  • ¥15 这个python五子棋代码的每一句意思是什么啊
  • ¥15 求一段隐藏代码,隐藏一些内容
  • ¥15 汇川EASY521plc电子凸轮
  • ¥15 C++ 如何判断设置快捷键来实现隐藏/显示窗口
  • ¥15 关于#材料工程#的问题:有没有具有电子阻挡层和空穴阻挡层的电池仿真silvaco代码例子或者其他器件具有阻挡层例子的silvaco代码(最好还有相关文献)
  • ¥60 基于MATLAB的TAOD算法
  • ¥15 Groimp使用疑问