如何在iphone上将图像,音频和文本上传到服务器

时间:2013-05-22 15:29:08

标签: iphone ios objective-c

我是手机编程的新手。使用下面的代码我上传图片,音频,用户ID,标题。但是这里userid没有上传。像userid这样的回复是空的。

 UIImage * img = [UIImage imageNamed:@"Elephant.jpg"];
    NSData *imageData = UIImageJPEGRepresentation(img,0.2);     //change Image to NSData
    NSString *file2 = [[NSBundle mainBundle] pathForResource:@"tone" ofType:@"mp3"];
    NSData *audioData = [NSData dataWithContentsOfFile:file2];
    NSString* captionn = [NSString stringWithFormat:@"Aslam"];
    int x = 45;
    NSString *user_idd =[NSString stringWithFormat:@"%i",x];
    NSString* mobile_tauky_idd = [NSString stringWithFormat:@"2"];
    NSString * filenames = [NSString stringWithFormat:@"TextLabel"];
    NSLog(@"%@", filenames);
    NSString *urlString = @"http://182.73.152.59:82/php/tauky_services/codeigniter-restserver-master/index.php/api/uploadClass/uploadTauky/";
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];   
    boundary = @"---------------------------14737809831466499882746641449";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
        [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
          NSLog(@"%@", request);
        NSMutableData *body = [NSMutableData data];
 [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"caption\"\r\n\r\n%@", captionn] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    //inputname
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"user_id\"\r\n\r\n%@", user_idd] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    //mobile_tauky_idd
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"mobile_tauky_id\"\r\n\r\n%@", mobile_tauky_idd] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    //Image
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"image\"; filename=\"%@\"\r\n",imageData] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"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]];
    //Audio
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"audio\"; filename=\"%@\"\r\n",audioData] dataUsingEncoding:NSUTF8StringEncoding]];
  //  [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"audio\"; filename=\"tone.mp3\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];


    [body appendData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"tone" ofType:@"mp3"]]];

    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
        NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
        NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
        NSLog(@"Response : %@",returnString);

        if([returnString isEqualToString:@"Success ! The file has been uploaded"])
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success" message:@"Image Saved Successfully" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];

            [alert show];
        }
        NSLog(@"Finish");

任何机构都可以告诉我这段代码中有什么错误

2 个答案:

答案 0 :(得分:0)

我做了很多服务器到app / app到服务器。我使用此代码上传照片。

-(void)uploadPhotoToServer
{
NSMutableString *myString = [[NSMutableString alloc] initWithString:@"http://www.domain.com/"];
NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:myString]
                                                               cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                           timeoutInterval:60];
[urlRequest setHTTPMethod:@"POST"];
NSString *boundary = @"----WebKitFormBoundaryQkZe6RUJZ2xbzXLB";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[urlRequest addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"picBLOB\" filename=\"image001.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/jpg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:photoData]]; // <<< photoData is a UIImage
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[urlRequest setHTTPBody:body];

NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection
 sendAsynchronousRequest:urlRequest
 queue:queue
 completionHandler: ^( NSURLResponse *response,
                      NSData *data,
                      NSError *error)
 {
     if (error == nil)
     {
         // no error... do something if needed.
     } else
     {
         // error... do something if needed.
     }
 }];
}

对于文字,我只是将其包含在我的网址中。像这样:

NSMutableString *myString = [[NSMutableString alloc] initWithString:@"http://www.domain.com/4DACTION/"];
    [myString appendString:@"userID45"];
    NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:myString]
                                                                cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                            timeoutInterval:60];

我的服务器端解析URL值。

注意:我使用的是4D服务器。 4DACTION特定于该服务器,可能不适用于您的服务器。

答案 1 :(得分:0)

错误可能在您的服务器上。你可以发布解析这些变量的代码部分吗? (抱歉,还没有能力对问题发表评论,所以我必须“回答”才能澄清)