iOS中的服务器上不会上传多个图像和音频

时间:2013-12-02 08:23:10

标签: ios iphone objective-c

我是iPhone编程的新手。使用下面的代码我尝试上传多个图像和音频以及用户ID,标题,服务器中的其他合并图像但它不工作前3个参数存储后多个音频和图像用于我正在逐个上传,但内部数据不在服务器上传我收到服务器的响应,如图像和音频没有上传。任何人都可以告诉我这段代码中的错误。

-(void)uploadblauky:(id)sender

{
     NSLog(@"SGDSGvzvSG");
    NSString *imagestr=[imageCollection4 objectAtIndex:3];
    // NSLog(@"%@sdffhgh",imagestr);
    spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [spinner setColor:[UIColor greenColor]];

    //[spinner setCenter:CGPointMake(150,150)]; // I do this because I'm in landscape mode
    spinner.frame = CGRectMake(0,0,35,35);
    //    activityIndicator = [[UIActivityIndicatorView alloc]
    // initWithFrame:CGRectMake(0.0f, 0.0f, 20.0f, 20.0f)];
    [sharethumb addSubview:spinner];
    [spinner startAnimating];



    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSString* bllaukycaption = @"BlaukyAslam";
        // NSString* user_id = @"45";
        NSString* user_id=[NSString stringWithFormat:@"2"];
        NSString* mobile_blauky_id = [NSString stringWithFormat:@"2 "];
        NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
        profileuserid = [[NSString alloc]initWithFormat:@"%@",[standardUserDefaults objectForKey:@"id"]];
        NSLog(@"%@newStr",profileuserid);


        NSURL *dataURL=[[NSURL alloc]initWithString:[NSString stringWithFormat:@"http://54.221.218.162/index.php/api/uploadClass/uploadBlauky/format/xml"]];
        NSMutableURLRequest *dataRqst = [NSMutableURLRequest requestWithURL:dataURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
        [dataRqst setHTTPMethod:@"POST"];
        NSString *stringBoundary = @"0xKhTmLbOuNdArY---This_Is_ThE_BoUnDaRyy---pqo";
        NSString *headerBoundary = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary];
        [dataRqst addValue:headerBoundary forHTTPHeaderField:@"Content-Type"];
        NSMutableData *postBody = [NSMutableData data];

        // -------------------- ---- caption ---------------------------\\


        [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [postBody appendData:[@"Content-Disposition: form-data; name=\"caption\"\r\n\r\n"
                              dataUsingEncoding:NSUTF8StringEncoding]];

        [postBody appendData:[bllaukycaption dataUsingEncoding:NSUTF8StringEncoding]];
        [postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        //caption
        // -------------------- ---- userId ---------------------------\\

        [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [postBody appendData:[@"Content-Disposition: form-data; name=\"user_id\"\r\n\r\n"
                              dataUsingEncoding:NSUTF8StringEncoding]];

        [postBody appendData:[profileuserid dataUsingEncoding:NSUTF8StringEncoding]];
        [postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        NSLog(@"%@",user_id);
        NSLog(@"Hai aslam");
        //asdf
        // -------------------- ---- mobile_tauky_id ---------------------------\\

        [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [postBody appendData:[@"Content-Disposition: form-data; name=\"mobile_tauky_id\"\r\n\r\n"
                              dataUsingEncoding:NSUTF8StringEncoding]];

        [postBody appendData:[mobile_blauky_id dataUsingEncoding:NSUTF8StringEncoding]];
        [postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        // -------------------- ---- blauky_id ---------------------------\\
           // -------------------- ---- MergeImage Upload Status ---------------------------\

        [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [postBody appendData:[@"Content-Disposition: form-data; name=\"thumbnail_blauky\"\r\n\r\n"
                              dataUsingEncoding:NSUTF8StringEncoding]];

        NSString *mediaType=@"Image";
        NSLog(@"type %@",mediaType);
        [postBody appendData:[mediaType dataUsingEncoding:NSUTF8StringEncoding]];
        [postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

        //pass MediaType file

        [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [postBody appendData:[@"Content-Disposition: form-data; name=\"thumbnail_blauky\"; filename=\"image18.png\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        [postBody appendData:[@"Content-Type: image/png\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        [postBody appendData:[@"Content-Transfer-Encoding: binary\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        // get the image data from main bundle directly into NSData object

        //  NSError *error;
        NSLog(@"%@sd",imagestr);
        NSLog(@"%@sdf",[imageCollection4 objectAtIndex:3]);
        //  NSData *audiofile1Data = [[NSData alloc]initWithContentsOfURL:surl];
        NSData *fileData1 = [[NSData alloc] initWithContentsOfFile:thumbnailmergeimage];
         // add it to body
        [postBody appendData:fileData1];
        [postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        NSLog(@"%@",arrayyes2);
        NSLog(@"%@",arrayyes);


       for(int r=0;r<[arrayyes2 count];r++)
       {

                  // -------------------- ---- Image Upload Status ---------------------------\

        [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [postBody appendData:[@"Content-Disposition: form-data; name=\"image\"\r\n\r\n"
                              dataUsingEncoding:NSUTF8StringEncoding]];

        NSString *mediaType=@"Image";
        NSLog(@"type %@",mediaType);
        [postBody appendData:[mediaType dataUsingEncoding:NSUTF8StringEncoding]];
        [postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

        //pass MediaType file

        [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [postBody appendData:[@"Content-Disposition: form-data; name=\"image\"; filename=\"image18.png\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        [postBody appendData:[@"Content-Type: image/png\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        [postBody appendData:[@"Content-Transfer-Encoding: binary\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        // get the image data from main bundle directly into NSData object
        NSString *imagestr=[arrayyes2 objectAtIndex:r];
        //  NSError *error;
        NSLog(@"%@sd",imagestr);
        NSLog(@"%@sdf",[imageCollection4 objectAtIndex:3]);
              NSLog(@"%@sdf",[arrayyes2 objectAtIndex:0]);
           NSLog(@"%@sdf",[arrayyes2 objectAtIndex:1]);

           NSLog(@"%i@sdf",[arrayyes2 count]);

           NSLog(@"%@sdf",[arrayyes2 objectAtIndex:2]);

        //  NSData *audiofile1Data = [[NSData alloc]initWithContentsOfURL:surl];
        NSData *fileData1 = [[NSData alloc] initWithContentsOfFile:imagestr];
        //   UIImage *img = [UIImage imageWithContentsOfFile:imagestr];
        //NSData *imgData = UIImagePNGRepresentation(Your Image);
        // add it to body
        [postBody appendData:fileData1];
        [postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];



        // -------------------- ---- Audio Upload Status ---------------------------\

        [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [postBody appendData:[@"Content-Disposition: form-data; name=\"image\"\r\n\r\n"
                              dataUsingEncoding:NSUTF8StringEncoding]];

        NSString *mediaTypee=@"tone";
        NSLog(@"type %@",mediaTypee);
        [postBody appendData:[mediaTypee dataUsingEncoding:NSUTF8StringEncoding]];
        [postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];


        [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [postBody appendData:[@"Content-Disposition: form-data; name=\"audio\"; filename=\"audiofilename.caf\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        [postBody appendData:[@"Content-Type: audio/caf\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        [postBody appendData:[@"Content-Transfer-Encoding: binary\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        NSString *audiosti=[arrayyes objectAtIndex:r];
           // NSLog(@"%@",audiosti);
        surl = [NSURL fileURLWithPath:audiosti];
            NSData *file1Data = [[NSData alloc] initWithContentsOfFile:audiosti];
       // NSLog(@"%@audiostring",surl);

           // add it to body
        [postBody appendData:file1Data];
        [postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

        [postBody appendData:[[NSString stringWithFormat:@"--%@--\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
       }

        // add body to post
       // NSLog(@"%@",postBody);
        [dataRqst setHTTPBody:postBody];

        NSHTTPURLResponse* response =[[NSHTTPURLResponse alloc] init];
        NSError* error = [[NSError alloc] init] ;

        //synchronous filling of data from HTTP POST response
        NSData *responseData = [NSURLConnection sendSynchronousRequest:dataRqst returningResponse:&response error:&error];

        //convert data into string
        responseString = [[NSString alloc] initWithBytes:[responseData bytes] length:[responseData length] encoding:NSUTF8StringEncoding];




        NSLog(@"Response String %@",responseString);

}

2 个答案:

答案 0 :(得分:0)

上传图片和注册

  imgData = UIImageJPEGRepresentation(imageData.image, 60);
 //////////////////////
 NSLog(@"%@%@%@%@",userName.text,eMail.text,passWord.text,country.text);
 NSString *str=[NSString stringWithFormat:@"%@registration.php",appdel.baseUrl];
 NSString *urlString = [NSString stringWithFormat:@"%@",str];

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

 [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
 [body appendData:[@"Content-Disposition: form-data; name=\"file\"; filename=\"a.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
 [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
 [body appendData:[NSData dataWithData:imgData]];
 [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

 //  parameter username

 [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
 [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"username\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

 [body appendData:[userName.text dataUsingEncoding:NSUTF8StringEncoding]];
 [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];


 //  parameter token
 [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
 [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"email\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

 [body appendData:[eMail.text dataUsingEncoding:NSUTF8StringEncoding]];
 [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];


 // parameter method
 [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
 [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"pass\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

 [body appendData:[passWord.text dataUsingEncoding:NSUTF8StringEncoding]];
 [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];


 //parameter method
 NSLog(@"%@",countryCode);
 [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
 [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"country\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

 [body appendData:[countryCode dataUsingEncoding:NSUTF8StringEncoding]];
 [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

 // close form
 [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];


 // setting the body of the post to the reqeust
 [request setHTTPBody:body];


 NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
 // NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
 NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:returnData options:NSJSONReadingMutableLeaves error:nil];
 //
 str_Status=[dict valueForKey:@"status"];
 NSLog(@"str stusua is :%@",str_Status);

///给出在webservice中给出参数的参数.......

答案 1 :(得分:0)

您好我正在使用以下代码将我的应用中的两张图片上传到java服务

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"yourServiceUrl"]];
[request setHTTPMethod:@"POST"];

NSString *boundary = [NSString stringWithFormat:@"--yourboundary"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];//initial boundary
[request setValue:contentType forHTTPHeaderField:@"Content-Type"];


NSMutableData *body = [NSMutableData data];

for(int i =0; i<=1;i++)
{
 [body appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
 [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n",arrNames[i],arrImageNames[i]] dataUsingEncoding:NSUTF8StringEncoding]];
 [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
 [body appendData:[NSData dataWithData:arrImgData[i]]];
 [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
}

[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; //final boundary

[request setHTTPBody:body];
[request setValue:[NSString stringWithFormat:@"%d",[body length]]  forHTTPHeaderField:@"Content-Length"];