2

我是电话编程的新手。使用下面的代码,我在服务器中上传标题、user_id、mobile_tauky_id、blauky_id、图像、音频但它没有上传,之后它在控制台中没有显示任何想法需要太长时间。

 UIImage * img = [UIImage imageNamed:@"Elephant.jpg"];
    NSData *imageData = UIImageJPEGRepresentation(img,0.2);     
        NSString *file2 = [[NSBundle mainBundle] pathForResource:@"ch" ofType:@"mp3"];
        NSData *audioData = [NSData dataWithContentsOfFile:file2];
        NSString* captionn = [NSString stringWithFormat:@"Aslam"];
        NSString* user_idd = [NSString stringWithFormat:@"1"];
        NSString* mobile_tauky_idd = [NSString stringWithFormat:@"2"];
        NSString* blauky_idd = [NSString stringWithFormat:@"0"];
        NSString *urlString = @" http://182.73.152.59:82/php/tauky_services/codeigniter-restserver-master/index.php/api/uploadClass/uploadTauky/";
         //[request setURL:[NSURL URLWithString:@" 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"];

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

        NSMutableData *body = [NSMutableData data];
   [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"caption\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[captionn dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        // text parameter
        [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"user_id\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[user_idd dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];


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



        // another text parameter
        [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"blauky_id\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[blauky_idd dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        // file
        [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithString:@"Content-Disposition: imageData; name=\"Elephant\"; filename=\".jpg\"\r\n"] 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 stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

        // file
        [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithString:@"Content-Disposition: audioData; name=\"ch\"; filename=\".mp3\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[NSData dataWithData:audioData]];
        [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        // close form
        [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

        // set request body
        [request setHTTPBody:body];
    NSLog(@"%@",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");

任何人都可以告诉我这段代码有什么错误。但我必须输入这个参数强制标题,user_id,mobile_tauky_id,blauky_id,图像,音频

4

1 回答 1

0

我正在使用 ASIFormDataRequest,它工作正常并在几秒钟内给出响应。

UIImage * img = [UIImage imageNamed:@"Elephant.jpg"];
NSData *imageData = UIImagePNGRepresentation(img);

NSString *strURL =YOUR URL";
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:strURL]];
[request setDelegate:self];
[request setPostValue:@"AVANVSJ767UYSUYAS" forKey:@"IMEI"];
[request setPostValue:CardName forKey:@"CustomerName"];
[request setPostValue:@"varun@gmail.com" forKey:@"CustomerEmailID"];
[request addData:imageData withFileName:@"IMAGE" andContentType:@"image/png" forKey:@"FileName1"];
[request setDelegate:self];
[request startAsynchronous];
于 2013-05-22T09:14:25.963 回答