在iOS上使用HTTP帖子发送图像和文本

时间:2015-04-02 13:13:16

标签: ios objective-c iphone xcode

我尝试使用HTTP帖子发送图像和一些文本信息,但由于某种原因,post方法不发送图像而只发送文本信息。

这是我的方法。

UIImage *image = self.imgBtnPhoto.imageView.image; //is a photo taken and viewed in button.

NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];

NSString *post = [NSString
                  stringWithFormat:
                  @"author=%@&comment=%@&comment_post_ID=%@&attachment=%@",
                  strUsername,
                  stredtComment,
                  strIDArgument,
                  imageData];



NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"https://myUrl.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

[request setHTTPBody:postData];

NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];

if(conn) {
    NSLog(@"Connection Successful"); //The post is sended and viewed in my WebSite without image!
} else {
    NSLog(@"Connection could not be made");
}

错误在哪里?

此致

2 个答案:

答案 0 :(得分:1)

你需要为图像设置标题和边界...查看下面的代码然后尝试一下......可能对你有用..

 //To convert your image file to raw.
NSString *urlStr = [NSString stringWithFormat:@"urlwithparams"];  

NSString *urlString = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];  

[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];  

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

//Your uploading file.
NSMutableData *bodyData = [NSMutableData data];
[bodyData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];  

/*
 * Declares the PHP received $_FILES variables.
 * - $_FILES['_varName']['name'] = _sample.png
 */

[bodyData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"sample_.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:[@"Content-Type: image/png\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

[bodyData appendData:[NSData dataWithData:imageData]];

[bodyData appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:bodyData];
NSLog(@"request-->%@",request);

//Received response of server.
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
//Dumps it.
NSLog(@"%@", returnString);

答案 1 :(得分:0)

你做错了,HTTP正文不能直接成为图像数据,你应该创建一个带标题的正文。

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]];
[body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"image\"\r\n"]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:postData];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];