我尝试使用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];
在服务器端:file 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)
任何人都可以帮助我吗?谢谢你。