简体   繁体   English

AFnetworking没有将图像上传到服务器

[英]AFnetworking is not uploading image to server

Hello i am new to afnetworking, i want to upload image at server path from ios sdk. 您好,我是afnetworking的新手,我想从ios sdk在服务器路径上上传图像。 below is code for afnetworking, 以下是用于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];

SERVER RESPONSE IS 服务器响应是

 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>}

Try out this for url. 试试这个URL。

 NSString *imgUrl=[imageURLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSURL *url=[NSURL URLWithString:imgUrl];

Also your path string is not proper i guess. 我猜你的路径字符串也不正确。 Hope that helps. 希望能有所帮助。

welll i am using this way and its working fine. 好吧,我正在使用这种方式,它的工作正常。

 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];

edited 已编辑

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"];

 }]; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM