E
E
Evgeny Khripunov2018-01-31 21:06:58
Yii
Evgeny Khripunov, 2018-01-31 21:06:58

Why are not all files uploaded if they are "drag and drop" one by one into the field?

It is possible to attach files to a post. Attaching is done using the widget https://github.com/kartik-v/yii2-widget-fileinput
If you select several files on your computer and drag them to the upload field, then everything works fine. If you drag and drop files one at a time, then for some reason only the last file is loaded, the rest are simply lost. Why is this happening and how to fix it?
The procedure itself is as follows: fill in the required fields, drag and drop files, press the save button. (The files themselves do not click download). The order is this, because I don't want the files to be uploaded without the created post.
Code in controller:

public function actionCreate()
    {
        $model = new Article(); //Создаю модель поста
        $upload = new UploadForm(); // Создаю модель загрузки файлов

        if (Yii::$app->request->isPost) {
            $model->load(Yii::$app->request->post()); //Загружаю данные в модель
            if($model->save()){ 
                $upload->files = UploadedFile::getInstances($upload, 'files'); // Если модель сохранена, то загружаю файлы
                $upload->UploadOperation($model->id); //Сохраняю файлы, а так же для каждого файла создаю запись в таблицу
                return $this->redirect(['view', 'id' => $model->id]);
            }
        }
        return $this->render('create', compact('model','categories','upload'));
    }

File upload model:
public $files;
    public function rules()
    {
        return [
            
    ); ?>

    <?= $form->field($model, 'content')->textarea(['rows' => 6]) ?> // Содержимое поста
<?= $form->field($upload, 'files[]')->widget(FileInput::className(), [  
        'language' => 'ru',
        'options' => ['multiple' => true],
        'pluginOptions' => ['uploadUrl' => Url::to(['Тут просто заглушка, т.к. файлы грузятся после создания поста'])] 
    ]); ?>
 <div class="form-group">
        <?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>  //Кнопка для создания поста
    </div>
    <?php ActiveForm::end(); ?>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question