B
B
beefront172017-06-05 13:42:00
Yii
beefront17, 2017-06-05 13:42:00

Yii2 file upload and output?

Good afternoon! Can you please tell me what could be the problem in this code?
This code should load and save files. (multiple uploads files)
View

<?php
use yii\helpers\Html;
use yii\helpers\Url;
use kartik\file\FileInput;

if (!$model->isNewRecord) {
    echo $form->field($files, 'files')->widget(FileInput::classname(), [
        'options' => [
            'multiple' => true
        ],
        'pluginOptions' => [
            'showCaption' => false,
            'showUpload' => false,
            'uploadUrl' => Url::to(['/ajax/clientuploadfile/', 'id' => $model->id]),
            'uploadExtraData' => [
                'user_id' => $model->id,
            ],
            'initialPreview' => $files->initialPreview($model->id),
            'initialPreviewConfig' => $files->initialPreviewConfig($model->id, Url::to(['/ajax/clientdeletefile/', 'user_id' => $model->id])),
            'overwriteInitial' => false,
            'maxFileCount' => 10,
            'fileActionSettings' => [
                'showZoom' => false,
                'showDrag' => false,
            ],
        ],

    ]);
}

Model
<?php

namespace app\models;

use Yii;
use yii\helpers\Html;
use yii\web\UploadedFile;
use yii\data\ActiveDataProvider;

/**
 * This is the model class for table "{{%files}}".
 *
 * @property string $id
 * @property string $user_id
 * @property string $name
 * @property string $created_at
 */
class Files extends \yii\db\ActiveRecord
{
    public $files;

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%files}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name'], 'string', 'max' => 100],
            [['file_name'], 'string', 'max' => 20],
            [['files'], 'file', 'skipOnEmpty' => true, 'maxFiles' => 20],
            [['created_at'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'user_id' => Yii::t('app', 'User ID'),
            'file_name' => Yii::t('app', 'Назва документу'),
            'name' => Yii::t('app', 'Назва документу'),
            'created_at' => Yii::t('app', 'Створено'),
            'files' => Yii::t('app', 'Файл'),
        ];
    }

    public static function loadModel($id)
    {
        if ($id) {
            $model = self::findOne($id);
            if ($model)
                return $model;
        }
        return new self;
    }

    public function getAll($id)
    {
        $query = self::find(['user_id' => $id]);
        return new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
    }

    public function initialPreviewConfig($id, $urldel)
    {
        $return_json = [];
        foreach ($this->getAll($id)->getModels() as $k => $file) {
            $return_json[] = [
                'caption' => $file->file_name,//$_SERVER['DOCUMENT_ROOT'] . '/web/uploads/docs/'.$id.'/' . $file->file_name,
                'size' => filesize($_SERVER['DOCUMENT_ROOT'] . '/web/uploads/docs/' . $id . '/' . $file->file_name),
                'url' => $urldel,
                'key' => $file->id,
            ];
        }
        return $return_json;
    }

    public function initialPreview($id)
    {
        $return_json = [];
        foreach ($this->getAll($id)->getModels() as $k => $file) {
            $return_json[] = '<div class="file-preview-frame" id="' . $file->id . '" data-fileindex="' . $k . '">' . $file->name . '</div>';
        }
        return $return_json;
    }
}

This is how it should be displayed, but nothing is displayed
<?= Yii::$app->controller->renderPartial('manager/create_form/_files2', ['form' => $form, 'model' => $model, 'files' => $files]); ?>

Please help me with this conclusion. Can someone please explain where is the mistake.
I would be very grateful for your help!

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