AFNetworking不上传图片

时间:2014-01-17 19:16:11

标签: ios image file-upload afnetworking afnetworking-2

我正在尝试通过AFnetworking上传图片。我能够获取图像URL,它确实与我的服务器联系。但是,它不会上传。文件上传文件夹为空,当我收到我的JSON响应时,它是“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);
}];

图片网址如下所示:

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

我似乎无法弄清楚我做错了什么..脚本很好,因为它适用于Android,就像它应该... ...

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 个答案:

答案 0 :(得分:1)

您的上传代码将文件命名为image,但您的脚本似乎需要filename。我暂时没有做任何PHP,但我认为它们应该匹配。

还有另一种方法可以让您指定有关要附加到表单数据的部件的更多详细信息,因此您可能需要设置相应的名称。