0

I am trying to upload an image via AFnetworking. I am able to get the image url, and it does contact my server. However, it won't upload. The file upload folder is empty and when I get back my JSON response, it is "null"

   - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];


ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
// Request to save the image to camera roll
[library writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation)[image imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
    if (error) {
        NSLog(@"error");
    } else {
        NSLog(@"url %@", assetURL);
        NSData *data = [NSData dataWithContentsOfURL:assetURL];
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        path = [path stringByAppendingString:@"/image.jpg"];
        [data writeToFile:path atomically:YES];
        [self uploadPhoto:path];
       // NSLog(path);
        [self dismissModalViewControllerAnimated:NO];
    }  
}];

}
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        NSDictionary *parameters = @{@"foo": self.targetid};
        NSURL *filePath = [NSURL fileURLWithPath:file];
       [manager POST:@"http:/****/uploadpics.php" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
       [formData appendPartWithFileURL:filePath name:@"image" error:nil];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

image url looks like this:

/var/mobile/Applications/FFCAE923-1115-4209-AB39-D9D1ACEB9CB7/Documents/yourLocalImage.png 

I can't seem to figure out what am I doing wrong.. The script is fine because it works for android just as it is supposed to...

PHP:

$name = $_FILES['filename']['name'];

if (is_uploaded_file($_FILES['filename']['tmp_name'])){
   if (move_uploaded_file($_FILES['filename']['tmp_name'], $folder.$_FILES ['filename']         ['name'])) {
     Echo  $foname;
 } else {

  }
} else {

}

1 Answer 1

1

Your upload code names the file image but your script seems to expect filename. I haven't done any php for a while but I think they should match.

There is another method which allows you to specify more details about the part that you're appending to the form data so you probably need that to set the appropriate names.

13
  • in php or Obj C?.. my head hurts from all these languages =(.. I am not sure if they should match .. i think filename is is appendPartWithFileURL ... will post it as http post files file name.. could be wrong but ..
    – Dnaso
    Jan 17, 2014 at 19:43
  • Change the obj-c. Use Charles proxy to see what is actually sent.
    – Wain
    Jan 17, 2014 at 21:15
  • @Wain,I actually narrowed it down with a check on the AFNetworking.. I am getting the following: b] Append part failed with error: Error Domain=AFNetworkingErrorDomain Code=-1000 "The operation couldn’t be completed. Expected URL to be a file URL" UserInfo=0x146429d0 {NSLocalizedFailureReason=Expected URL to be a file URL apparently the location of my image can't be parsed as an NSURL. but its int the var/folder, isnt that ok? other wise do you know how i can put it in the file:/// directory?
    – Dnaso
    Jan 17, 2014 at 21:29
  • Looks ok. Did you log the URL? Is it actually nil for some reason?
    – Wain
    Jan 17, 2014 at 21:36
  • @Wain no the url is not nil the url is: /var/mobile/Applications/FFCAE923-1115-4209-AB39-D9D1ACEB9CB7/Documents/yourLocalImage.png but I log it before converting it into a NSURL. I am going to try to log it as NSURL see if it comes up the same.
    – Dnaso
    Jan 19, 2014 at 17:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.