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");
}
错误在哪里?
问候
谢谢,我用你的回答和解决的另一个问题 http://stackoverflow.com/questions/8564833/ios-upload-image-and-text-using-http-post 现在一切工作正常。 非常感谢您的即时回复;) – Bruno 2015-04-02 13:38:56