我在解析文件时遇到一些问题

时间:2019-04-14 12:08:10

标签: yii

我在文件解析方面遇到一些问题,我是Yii的新手,我不太了解它,我必须从磁盘上选择一个xls文件并进行解析以显示其上的数据,我被困在这里:

这是我的观点:

<?php
use yii\helpers\Html;
use yii\widgets\LinkPager;
use yii\widgets\ActiveForm;
?>
<h1>Upload a file</h1>

<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>

<?= $form->field($model, 'file')->fileInput() ?>

<?php ActiveForm::end(); ?>

<?php echo $model->file ?>

这是控制器:

<?php

namespace app\controllers;

use yii\web\Controller;
use yii\data\Pagination;
use app\models\CatalogStudenti;
use app\models\UploadForm;
use Yii;


use yii\web\UploadedFile;
class SiteController extends Controller
{   


  public function actionIndex()
    {
        $model = new UploadForm();

        if (Yii::$app->request->isPost) {
            $model->file = UploadedFile::getInstance($model, 'file');
            if ($model->upload()) {
                // file is uploaded successfully
                return;
            }
        }

        return $this->render('index', ['model' => $model]);
    }
}

这是模型:

<?php


namespace app\models;

use yii\base\Model;
use yii\web\UploadedFile;

class UploadForm extends Model
{
    /**
     * @var UploadedFile
     */
    public $file;

    public function rules()
    {
        return [
            [['file'], 'file', 'skipOnEmpty' => false, 'extensions' => 
     'xls, xlsx'],
        ];
    }

    public function upload()
    {
        if ($this->validate()) {
            $this->file->saveAs('uploads/' . $this->file->baseName . '.' 
         . $this->file->extension);
            return true;
        } else {
            return false;
        }
    }
}

我不知道如何将数据从视图发送到控制器进行解析,然后再返回视图以显示该数据

0 个答案:

没有答案