我希望能够将照片上传到我的网站,一旦完成,就可以找到另一个视图控制器。如果它出现错误xyz:
像:
if (!error) {
[self performSegueWithIdentifier:@"tocity&country" sender:self];
}
else
{
NSError *error = [request error];
}
所以目标c代码:
NSData *imageData = UIImagePNGRepresentation([UIImage imageNamed:@"image.jpg"]);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
initWithURL:[NSURL
URLWithString:@"http://******.co.uk/****/imageupload.php"]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:20.0];
[request setHTTPMethod:@"POST"];
[request setValue:@"image/jpg"
forHTTPHeaderField:@"Content-type"];
[request setValue:[NSString stringWithFormat:@"%lu",
(unsigned long)[imageData length]]
forHTTPHeaderField:@"Content-length"];
[request setHTTPBody:[self imageDataToSend]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if( theConnection )
{
[[NSMutableData data] retain];
}
else
{
NSLog(@"theConnection is NULL");
}
[theConnection release];
和imageupload.php
:
<?php
$handle = fopen("image.jpg", "wb"); // write binary
fwrite($handle, $HTTP_RAW_POST_DATA);
fclose($handle);
print "Received image file.";
?>
我也注意到这样的iphone屏幕顶部没有加载gif,上传图片时怎么样?:
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSURL *URL = [NSURL URLWithString:@"http://******.co.uk/****/imageupload.php"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
//where does the http code come in?
NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"];
NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithRequest:request fromFile:filePath progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
if (error) {
NSLog(@"Error: %@", error);
} else {
NSLog(@"Success: %@ %@", response, responseObject);
}
}];
[uploadTask resume];
答案 0 :(得分:1)
您的代码存在不同的问题。
如上所述,活动指示器不会自动出现在iOS上。你必须用
来调用它[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
隐藏它再次调用相同的方法(使用NO
参数)。
PS:不相关,但是......在您的代码中,我看不到文件上传的身份验证。这允许每个人使用任何工具(包括简单的cURL命令)将文件上传到您的服务器。这就是你想要的吗?
答案 1 :(得分:1)
始终添加检查以确保您确实获得了某些内容,
您可以使用is_uploaded_file试试这样:
<?php
if(is_uploaded_file($_FILES['image']['tmp_name'])){
$folder = "uploads/";
$file = basename( $_FILES['image']['name']);
$full_path = $folder.$file;
if(move_uploaded_file($_FILES['image']['tmp_name'], $full_path)) {
echo '{"success":true, "msg": "succesful upload, we have an image!"}';
} else {
echo '{"success":false, "msg": "upload received! but process failed"}';
}
}else{
echo '{"success":false, "msg": "upload failure ! Nothing was uploaded"}';
}
?>
NSData *imageData = UIImagePNGRepresentation([UIImage imageNamed:@"image.jpg"]);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
initWithURL:[NSURL
URLWithString:@"http://******.co.uk/****/imageupload.php"]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:20.0];
[request setHTTPMethod:@"POST"];
[request setValue:@"image/jpg" forHTTPHeaderField:@"Content-type"];
[request setValue:[NSString stringWithFormat:@"%lu",
(unsigned long)[imageData length]]
forHTTPHeaderField:@"Content-length"];
[request setHTTPBody:[self imageDataToSend]];
//Send the Request
NSData* returnData = [NSURLConnection sendSynchronousRequest: request
returningResponse: nil error: nil];
//serialize to JSON
NSDictionary *result = [NSJSONSerialization JSONObjectWithData:returnData options:NSJSONReadingMutableContainers error:nil];
//parsing JSON
bool success = [result[@"success"] boolValue];
if(success){
NSLog(@"Success=%@",result[@"msg"]);
}else{
NSLog(@"Fail=%@"result[@"msg"]);
}