解析新文件的最佳方法是挂钩到 uploadfield 保存方法,对于 FileIframeField 你可以通过子类化并覆盖 save()
(在 SilverStripe 3 中有一个名为 UploadField 的新类,在 UploadField 中您需要覆盖UploadField->upload(SS_HTTPRequest $request)
,并且那里的文件可以像这样访问$tmpfile = $request->postVar($this->getName());
:)
下面,以及如何在 FileIframeField 中执行此操作的示例:
class myFileIFrameField extends FileIFrameField {
public function save($data, $form) {
if (
!isset($data['FileSource'])
|| ($data['FileSource'] == 'new' && (!isset($_FILES['Upload']) || !$_FILES['Upload']))
|| ($data['FileSource'] == 'existing' && (!isset($data['ExistingFile']) || !$data['ExistingFile']))
) {
$form->sessionMessage(_t('FileIFrameField.NOSOURCE', 'Please select a source file to attach'), 'required');
Director::redirectBack();
return;
}
$fileContent = false;
if($data['FileSource'] == 'new') {
$fileContent = file_get_contents($_FILES['Upload']['tmp_name']);
}
elseif($data['FileSource'] == 'existing') {
$fileObject = DataObject::get_by_id('File', $data['ExistingFile']);
$fileContent = file_get_contents($fileObject->getFullPath());
}
if ($fileContent) {
// parse the $fileContent here
}
// if you want to still save the file into a relation,
//meaning if you want to have the actually FileIframeField behaviour still in tact then call
return parent::save($data, $form);
// other wise, if you do not want to save the relation and you don't want to save the file to the server
// thenn do NOT call parent::save, just do:
// Director::redirectBack();
}
}