您好我是afnetworking的新手,我想从ios sdk上传服务器路径上的图片。下面是afnetworking的代码,
NSData *imageToUpload = UIImageJPEGRepresentation(selectedImageView.image, 90);
AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://192.168.0.100//smart_attendance/api/employeeImage.php"]];
NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:@"employee_images/large/" 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];
服务器响应是
AFNetworkingOperationFailingURLRequestErrorKey=<NSMutableURLRequest
http://192.168.0.102/trackingwebsite/tree_images/large/>,
NSErrorFailingURLKey=http://192.168.0.102/trackingwebsite/tree_images/large/,
NSLocalizedDescription=Expected status code in (200-299), got 404,
AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x98671e0>}
答案 0 :(得分:0)
尝试使用此网址。
NSString *imgUrl=[imageURLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url=[NSURL URLWithString:imgUrl];
我猜你的路径字符串也不合适。 希望有所帮助。
我正在使用这种方式并且工作正常。
NSString* imageName = @"_image.png";
[[AFAPIClient sharedClient]initWithBaseURL:[NSURL URLWithString:BaseUrl]];
NSMutableURLRequest *request = [[AFAPIClient sharedClient] multipartFormRequestWithMethod:@"POST" path:[NSString stringWithFormat:@"%@%@",BaseUrl,sign_up.php] parameters:dict constructingBodyWithBlock: ^(id <AFMultipartFormData>formData)
{
[formData appendPartWithFileData:UIImagePNGRepresentation([dict objectForKey:@"image"]) name:@"image" fileName:imageName mimeType:@"image/png"];
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
{
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
}];
[[AFAPIClient sharedClient]enqueueHTTPRequestOperation:operation];
[operation release];
编辑
AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://192.168.0.100/smart_attendance/api/employeeImage.php"]];
NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:@"http://192.168.0.100/smart_attendance/api/employeeImage.php"
parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
[formData appendPartWithFileData:imageToUpload name:@"file" fileName:@"temp.jpeg" mimeType:@"image/jpeg"];
}];