2017-01-03 80 views
1

我想單個文件上傳使用Kartik的FileInput。通過標準執行此操作時Yii2:結合Kartik的FileInput和Kartik的DetailView

事情去細創建形式爲以下收益非空

$filedata = UploadedFile::getInstance($model, 'filedata'); 

進入卡爾蒂克的的DetailView在編輯模式下,試圖更新但它總是返回文件。

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,所以任何人確認這一點,並提供一些細節應該是受歡迎的)。

回答

0

當然,無論是在_form.phpview.php需要的multipart/form-data的爲好,如下:

'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>, 
    ], 
    ], 
], 
.... 
]