我尝试使用Kartik的FileInput进行单文件上传。
通过标准创建表单执行此操作时,一切顺利,因为以下内容返回非空:
$filedata = UploadedFile::getInstance($model, 'filedata');
然而,在编辑模式下进入Kartik的DetailView并尝试更新文件时,它总是返回 null 。
在view.php
我有:
[
'attribute' => 'filedata',
'visible' => Yii::$app->user->can('doIt'),
'type' => DetailView::INPUT_FILEINPUT,
'rowOptions' => ['class' => 'kv-view-hidden'],
'widgetOptions' => ['options' => ['accept' => 'application/pdf'],
'pluginOptions' => [
'showUpload' => false,
'allowedFileExtensions' => ['pdf'],
'initialCaption' => $model->filename,
],
'pluginEvents' => [
'filecleared' => <whatever>,
'fileloaded' => <whatever>,
],
],
],
在_form.php
(确实有效)时:
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
<?= $form->field($model, 'filedata')->widget(FileInput::classname(), [
'options' => ['accept' => 'application/pdf'],
'pluginOptions' => [
'showUpload' => false,
'allowedFileExtensions' => ['pdf'],
'initialCaption' => $model->getOldAttribute('filename'),
],
'pluginEvents' => [
'filecleared' => <whatever>,
'fileloaded' => <whatever>,
],
])
?>
有什么想法吗? (顺便说一句,不要知道'multipart/form-data'
中view.php
是否需要_form.php
,因为在tab
中,所以任何确认此事并提供一些细节的人都应该受到欢迎。
答案 0 :(得分:0)
当然,_form.php
和view.php
也需要多部分/表单数据,如下所示:
'formOptions' => ['options' => ['enctype' => 'multipart/form-data']],
'attributes' => [
...
[
'attribute' => 'filedata',
'visible' => Yii::$app->user->can('doIt'),
'type' => DetailView::INPUT_FILEINPUT,
'rowOptions' => ['class' => 'kv-view-hidden'],
'widgetOptions' => ['options' => ['accept' => 'application/pdf'],
'pluginOptions' => [
'showUpload' => false,
'allowedFileExtensions' => ['pdf'],
'initialCaption' => $model->filename,
],
'pluginEvents' => [
'filecleared' => <whatever>,
'fileloaded' => <whatever>,
],
],
],
....
]