文件管理器将文件保存到自定义表中



我使用的是Moodle 3.1+。我使用了filepicker上传文件。它运行良好。但是filepicker只显示上传的文件名。我想在文件选择器中显示上传的图像,但这似乎是不可能的。另一种选择是使用filemanager,但它需要额外的参数,比如contextid,在我的情况下没有。我正在将上传的图像路径插入我创建的表中。那么,如何使用filemanager将文件保存到自定义表中呢?

file_save_draft_area_files($data->attachments, $context->id, 'mod_glossary', 'attachment',
$entry->id, array('subdirs' => 0, 'maxbytes' => $maxbytes, 'maxfiles' => 50));

首先,在表单中创建一个filemanager元素:

$filemanageropts = $this->_customdata['filemanageropts'];
$mform->addElement('filemanager', 'attachment',get_string('displayedcontent', 'block_helloworld'), null, $filemanageropts);

注意:附件与我的自定义表中的名称相同。因此,该字段将填充文件itemid。

然后,在您的表单逻辑脚本中,将文件保存到您的自定义区域:

//form has been submitted
file_save_draft_area_files($form_submitted_data->attachment, $context->id, 'block_helloworld', 'attachment',
$form_submitted_data->attachment, array('subdirs' => 0, 'maxbytes' => 500000, 'maxfiles' => 1));

然后,当您加载表单数据时:准备MOODLE中所称的草稿区域,使用:

$draftitemid = $helloworldpage->attachment ; 
file_prepare_draft_area($draftitemid, $context->id, 'block_helloworld', 'attachment', $helloworldpage->attachment,
array('subdirs' => 0, 'maxbytes' => 5000000, 'maxfiles' => 1));

资源:

  • 官方文件API文档
  • 回购示例

最新更新