简体   繁体   中英

AFnetworking is not uploading image to server

Hello i am new to afnetworking, i want to upload image at server path from ios sdk. below is code for 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.

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

 }]; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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