我想上传文件,我必须将表单元素类型用作“文件”。在提交功能中,我无法获取与我正在上传的文件有关的任何适当数据。有人可以帮我解决这个问题吗?请找到以下代码:
public function buildForm(array $form, FormStateInterface $form_state) {
$form['test_CERTIFICATE'] = [
'#type' => 'file',
'#title' => $this->t('Certificate'),
'#description' => $this->t('Your Certificate (.pem file)').': '.\Drupal::state()->get('test_CERTIFICATE_NAME'),
];
return parent::buildForm($form, $form_state);
}
提交功能:
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$validators = array('file_validate_extensions' => array('pem'));
$files = file_save_upload('test_CERTIFICATE', $validators, 'public://certfiles', FILE_EXISTS_REPLACE);
$file = File::load($files[0]);
if($file) {
kint($files); exit;
//here control is not coming
$file->setPermanent();
$file->save();
}
}
请参阅github存储库以获取带有文件上传的自定义表单。github.com/nitin-kawane/...
—
尼廷