图像未通过 IOS 中的 POST 方法发送到 PHP Web 服务
Posted
技术标签:
【中文标题】图像未通过 IOS 中的 POST 方法发送到 PHP Web 服务【英文标题】:Image is not getting sent to the PHP web service through POST method in IOS 【发布时间】:2014-02-15 05:17:24 【问题描述】:我们有一个 php Web 服务,可以将图像附加到电子邮件中,因为它正在将图像收集到全局文件数组中。传输数据的方法是 POST 方法。我正在发送图像,但它没有通过 POST 方法收集到 Web 服务中。谁能告诉我问题出在网络服务还是我的代码中。
下面是我用来发送图像的代码。
NSString *tempString = [NSString stringWithFormat:@"sub1_fname=%@&sub1_lname=%@&sub1_email=%@&sub1_phone=%@&sub1_address=%@&sub1_city=%@&sub1_zip=%@&sub2_storage=%@&sub2_boxes=%@&sub2_oneman=%@&sub2_twoman=%@&sub2_heavy=%@&sub2_comments=%@&sub3_findus=%@&sub3_word=%@&sub3_praise=%@",f31fname,f31lname,f31email,f31phone,f31address,f31city,f31zip,f32storage,f32boxes,f32oneman,f32twoman,f32heavy,f32comments,f33findus,f33word,f33praise];
UIImage *image = [UIImage imageNamed:@"textlogo.png"];
NSMutableURLRequest *request =[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://ommultiengg.com/form/form3.php"]];
[request setHTTPMethod:@"POST"];
NSString *postString =[NSString stringWithFormat:@"\"sub2_image\":\"%@\"",image];//post the image
[request setValue:[NSString
stringWithFormat:@"%lu", (unsigned long)[postString length]]
forHTTPHeaderField:@"Content-length"];//get image length
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
[request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[tempString dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
【问题讨论】:
你不能直接将图片存储到网络服务器,你需要遵循3个步骤,1. 图片到NSData 2. NSData 到NSString 3. NSString 被传递到服务器 你可以使用这个可以在任何类型的网络服务上工作的库:github.com/mineshpurohit/ServiceCallingUtility 【参考方案1】:使用以下代码,您可以传递参数以及图像数据。
NSString *urlString = [NSString stringWithFormat:@"http://myAPIName/MethodName/test.php&username=%@&password=%@&image=%@&answer=%@&question_id=%@", username, password, imageName, answer, questionID];
NSLog(@"MyURL: %@",urlString);
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
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--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSString *str=[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"SourceImage\"; filename=\"Image_%@\"\r\n",[imagePath lastPathComponent]];
[body appendData:[[NSString stringWithString:str] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithContentsOfFile:imagePath]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
【讨论】:
以上是关于图像未通过 IOS 中的 POST 方法发送到 PHP Web 服务的主要内容,如果未能解决你的问题,请参考以下文章