0

我尝试使用 AFNetworking 将 UIImagePickerController 中的图像上传到 FTP 服务器,我使用了以下代码:

url = [NSURL URLWithString:@"ftp://user:pass@server"];
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];

    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];

    UIImage *imageIWantToUpload = imageupload;
    NSData *dataToUpload = UIImageJPEGRepresentation(imageIWantToUpload, 0.5);


    // Create the NSData object for the upload process

    NSMutableURLRequest *myRequest = [httpClient multipartFormRequestWithMethod:@"POST" path:@"upload.php"
    parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
                                                                     [formData appendPartWithFileData:dataToUpload name:@"uploadedfile" fileName:[NSString stringWithFormat:@"%@.jpg",[def valueForKey:@"cUsuario"]] mimeType:@"images/jpeg"];
                                                                 }];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:myRequest];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"success: %@", operation.responseString);
    }
    failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    // If access token is null , we ask to the user if he wants to sign in or sign up ???
    NSLog(@"error: %@",  operation.responseString);
    }
     ];
    [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
        NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
    }];
    NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
    [queue addOperation:operation];

在服务器端:文件upload.php

<?php
$filename="uploaded";
$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}
?%gt;

当我运行它时,我在控制台中收到的是 2013-06-25 15:57:33.566 Led-Camera[2644:907] 错误:(空)

有谁能够帮我?谢谢提前。

4

0 回答 0