2015-04-02 41 views
1

我试图发送图片和一些文字信息与HTTP张贴,但由于某些原因后发送方法不发送图像,但只有文字信息。发送图片和文字在iOS上的HTTP发帖

这是我的方法。

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

错误在哪里?

问候

回答

0

你需要设置页眉和boundry图像...看看下面的代码,并试图出来...可能为你工作..

//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); 
+0

谢谢,我用你的回答和解决的另一个问题 http://stackoverflow.com/questions/8564833/ios-upload-image-and-text-using-http-post 现在一切工作正常。 非常感谢您的即时回复;) – Bruno 2015-04-02 13:38:56

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