1

我正在尝试使用 Kartik 的 FileInput 上传单文件。

通过标准创建表单执行此操作时一切正常,因为以下返回non-null

$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因此欢迎任何人确认这一点并提供一些细节)。

4

1 回答 1

0

当然,multipart/form-data 也是需要的_form.phpview.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>,
    ],
  ],
],
....
]   
于 2017-01-13T00:19:03.770 回答