AFNetworking上传图片
问题描述:
我看了几个例子,但我认为我的问题可能与在PHP中。我试图从iphone上使用AFNetworking将图像上传到服务器。这里是我的OBJ-C代码:AFNetworking上传图片
-(IBAction)uploadButtonClicked:(id)sender
{
NSData *imageToUpload = UIImageJPEGRepresentation(mainImageView.image, 90);
AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://www.THESERVER.com"]];
NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:@"/PROJECT/upload.php" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
[formData appendPartWithFileData: imageToUpload name:@"file" fileName:@"temp.jpeg" mimeType:@"image/jpeg"];
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *response = [operation responseString];
NSLog(@"response: [%@]",response);
[MBProgressHUD hideHUDForView:self.view animated:YES];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[MBProgressHUD hideHUDForView:self.view animated:YES];
if([operation.response statusCode] == 403){
NSLog(@"Upload Failed");
return;
}
NSLog(@"error: %@", [operation error]);
}];
[operation start];
}
这是我的upload.php的:
function upload(){
$uploaddir = '/uploads/';
$file = basename($_FILES['file']['name']);
$uploadfile = $uploaddir . $file;
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
sendResponse(200, 'Upload Successful');
return true;
}
sendResponse(403, 'Upload Failed');
return false;
}
当我试图把它上传失败在
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
,并返回403 /假我设置的状态代码
最有可能的权限问题。启用'error_reporting(E_ALL)' –
因为我正在通过iOS设备执行此操作,所以我如何传回错误消息?我有一个我可以使用的函数sendResponse('400',“$ errors”),但是我该如何设置$ errors? – mkral
@mkral:我知道这是有点老,但我试图实现上传部分的代码的修改版本,它并没有为我工作 它到底是否为你工作? ps im试图学习ios/objective C –