Ios AFnetworking未将映像上载到服务器
你好,我是afnetworking新手,我想从ios sdk上传服务器路径上的图像。下面是AFN网络的代码Ios AFnetworking未将映像上载到服务器,ios,objective-c,post,afnetworking,Ios,Objective C,Post,Afnetworking,你好,我是afnetworking新手,我想从ios sdk上传服务器路径上的图像。下面是AFN网络的代码 NSData *imageToUpload = UIImageJPEGRepresentation(selectedImageView.image, 90); AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://192.168.0.100//smart_attendance/
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];
NSData*imageToUpload=uiimagejpegresentation(选择edimageview.image,90);
AFHTTPClient*客户端=[AFHTTPClient客户端WithBaseURL:[NSURL URLWithString:@]http://192.168.0.100//smart_attendance/api/employeeImage.php"]];
NSMutableURLRequest*request=[客户端multipartFormRequestWithMethod:@“POST”路径:@“employee_images/large/”参数:nil constructingBodyWithBlock:^(id formData){
[formData appendPartWithFileData:ImageToPload名称:@“file”文件名:@“temp.jpeg”mimeType:@“image/jpeg”];
}];
AFHTTPRequestOperation*操作=[[AFHTTPRequestOperation alloc]initWithRequest:request];
[操作setCompletionBlockWithSuccess:^(AFHTTPRequestOperation*操作,id响应对象){
NSString*response=[operation responseString];
NSLog(@“响应:[%@]”,响应);
//[MBProgressHUD hideHUDForView:self.view动画:是];
}失败:^(AFHTTPRequestOperation*操作,NSError*错误){
//[MBProgressHUD hideHUDForView:self.view动画:是];
如果([operation.response statusCode]==403){
NSLog(@“上传失败”);
返回;
}
NSLog(@“错误:%@,[操作错误]);
}];
[操作启动];
服务器响应为
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>}
AFNetworkingOperationFailingURLRequestErrorKey=,
N错误失效键=http://192.168.0.102/trackingwebsite/tree_images/large/,
NSLocalizedDescription=预期状态代码在(200-299)中,得到404,
AFNetworkingOperationFailingURLResponseErrorKey=}
试试这个url
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];
NSString*imageName=@“\u image.png”;
[[AFAPIClient sharedClient]initWithBaseURL:[NSURL URLWithString:BaseUrl]];
NSMutableURLRequest*request=[[AFAPIClient sharedClient]multipartFormRequestWithMethod:@“POST”路径:[NSString stringWithFormat:@“%@%@”,BaseUrl,sign_up.php]参数:dict constructingBodyWithBlock:^(id formData)
{
[formData appendPartWithFileData:UIImagePNgPresentation([dict objectForKey:@“image”])名称:@“image”文件名:imageName mimeType:@“image/png”];
}];
AFHTTPRequestOperation*操作=[[AFHTTPRequestOperation alloc]initWithRequest:request];
[操作setCompletionBlockWithSuccess:^(AFHTTPRequestOperation*操作,id响应对象)
{
}
失败:^(AFHTTPRequestOperation*操作,NSError*错误)
{
}];
[[AFAPIClient sharedClient]排队HttpRequestOperation:operation];
[操作释放];
编辑
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"];
}];
AFHTTPClient*client=[AFHTTPClient-clientWithBaseURL:[NSURL-URLWithString:@]http://192.168.0.100/smart_attendance/api/employeeImage.php"]];
NSMutableURLRequest*request=[客户端multipartFormRequestWithMethod:@“POST”路径:@”http://192.168.0.100/smart_attendance/api/employeeImage.php"
参数:nil constructingBodyWithBlock:^(id formData){
[formData appendPartWithFileData:ImageToPload名称:@“file”文件名:@“temp.jpeg”mimeType:@“image/jpeg”];
}];
您得到了什么响应或错误?有时它说所有参数都未传递,如果我更改了文件路径,有时它说此服务器上找不到url我认为您的url构造很奇怪,请看一看此文档的URL构造部分:。您应该检查您的基本URL和路径是否正确请查看响应我不知道如何构造正确的URL及其路径您的路径就是您正在访问的服务URL。表示web服务URL??是的,就像您的一样请尝试。AFHTTPClient*客户端=[AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@;NSMutableURLRequest*request=[client multipartFormRequestWithMethod:@“POST”路径:@“employee_images/large”参数:nil constructingBodyWithBlock:^(id formData){[formData appendPartWithFileData:imageToUpload名称:@“file”文件名:@“temp.jpeg”mimeType:@“image/jpeg”];];AFHTTPClient*客户端=[AFHTTPClient客户端WithBaseURL:[NSURL URLWithString:@];NSMutableURLRequest*请求=[client multipartFormRequestWithMethod:@“POST”路径:[NSString stringWithFormat:@“%@”参数:nil constructingBodyWithBlock:^(id formData){[formData appendPartWithFileData:imageToUpload name:@“file”文件名:@”temp.jpeg“mimeType:@“image/jpeg”];}];