UIImagePickerController で取得したばかりの画像を AFNetworking を使用して 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] エラーが表示されました: (null)
誰でも私を助けることができますか?よろしくお願いします。