首先我展示我的相关代码:
将UIImage转换为NSData:
imageData = UIImagePNGRepresentation(myImage);
然后我写了NSMutableRequest:
NSString *urlString = @"http://136.206.46.10/~katie_xueke/test.php";
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init]autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:30.0f];
[request setHTTPMethod:@"POST"];
然后我写了NSMutableData:
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSMutableData *body = [NSMutableData data];
//Image
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name =\"image\";filename=\"%@\"\r\n",imageName] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type:application/octet-stream\r\n\r\n"]dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Length: %@\r\n",postLength]dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"--%@--",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
$$问题是:NSData似乎无法发送到服务器端,并且在控制台窗口中显示的正文部分是这样的:
Content-Disposition: form-data; name ="image";filename="2012:03:06 15:06:48"
Content-Type:application/octet-stream
Content-Length: 164692
‰PNG
如何将uiimage的nsdata更改为二进制数据以便发送到php?
PS:我尝试过Uint8方法
UInt8 *rawData = [imageData bytes];
在php方面:
$uploaddir = './upload/';
echo "recive a image";
$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir . $file;
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "/uploads/{$file}";
}
我从其他地方复制了它,我不知道如何在网页上显示我的POST图像。
有人可以帮助我吗?
非常感谢。
答案 0 :(得分:2)
最后,我自己想出了解决方案。
AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://XXXX"]];
NSMutableURLRequest *myRequest = [client multipartFormRequestWithMethod:@"POST" path:@"upload.php" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
[formData appendPartWithFileData:imageData name:@"uploadedfile" fileName:dateTime mimeType:@"images/png"];
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:myRequest];
[operation setUploadProgressBlock:^(NSInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) {
NSLog(@"Sent %d of %d bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];
[operation setCompletionBlock:^{
NSLog(@"response string: %@", operation.responseString); //Lets us know the result including failures
}];
NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
[queue addOperation:operation];
我使用了AFNetworking而不是ASIHttpRequest。
在php方面,我的代码:
<?php
$filename="uploaded";
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
?>
谢谢那些帮助过我的人。
PS:非常感谢这位真正帮助过我的人: http://6foot3foot.com/developer-journal/afnetworking-php