0

我是电话编程的新手。使用下面的代码我正在上传图像、音频、用户 ID、标题。但是这里的用户 ID 没有上传。它给出的响应就像用户 ID 是空的。

 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");

任何人都可以告诉我这段代码有什么错误吗

4

2 回答 2

0

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

-(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.
     }
 }];
}

对于文本,我只是将其包含在我的 URL 中。像这样:

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 特定于该服务器,可能不适用于您的服务器。

于 2013-05-22T16:34:33.570 回答
0

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

于 2013-05-22T21:11:41.167 回答