Dnaso
Dnaso

Reputation: 1365

AFNetworking not uploading image

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 {

}

Upvotes: 0

Views: 1068

Answers (1)

Wain
Wain

Reputation: 119021

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.

Upvotes: 1

Related Questions