编程语言
首页 > 编程语言> > php – Yii2- view在提交excel文件后给出错误

php – Yii2- view在提交excel文件后给出错误

作者:互联网

我正在研究Yii2.我正在导入一个excel文件.导入它时,我也发送了以前型号的ID.过程如下

>用户打开GUI并从下拉列表中选择一些值,然后单击“创建”按钮.

创建控制器

 public function actionCreate()
  {
    $model = new MeterAcceptanceHeader();
    $model->prepared_by = Yii::$app->user->id;
    $model->prepared_at = date('Y-m-d H:i:s');

    if ($model->load(Yii::$app->request->post()) && $model->save()) {
        return $this->redirect(['excel','id'=>$model->id]);
    } else {
        return $this->render('create', [
            'model' => $model,
        ]);
    }
}

>点击下一个按钮后,用户将看到一个新窗口,要求他上传excel文件.

Excel控制器

public function actionExcel($id){

$file_name = "excel_" . Yii::$app->user->id . ".xlsx";

$error = "";
if(isset($_FILES['file'])) {
    $path_parts = pathinfo($_FILES["file"]["name"]);
    $extension = $path_parts['extension'];

    if(!in_array($extension,['xlsx','xls'])){

        $error = "Invalid file";
    }else {
        if (move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $file_name)) {
            $this->redirect([
                'process',
                'file_name' => $file_name,
                'header_no' => $_POST['header_no'],
                'id'=>$id
            ]);
        }
    }
 }
   return $this->render("excel",['error'=>$error,'id'=>$id]);
 }

Excel视图

  <div class="box-body">
    <?php if($error != ""){?>
  <div class="alert alert-danger"><?=$error?></div>
    <?php } ?>
  <form action="" method="post" enctype="multipart/form-data">

    <input type="hidden" name="_csrf" value="<?= Yii::$app->request->getCsrfToken() ?>"/>
    <input id="btn" class="form-control" type="file" name="file" />
    <div class="form-group">
        <br />
    Header Row <br />
    <select name="header_no" class="form-control">
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="7">7</option>
        <option value="8">8</option>
    </select>
    <br />
    <input type="submit" value=" Next " class="btn btn-primary" />
    </div>
     </form>

    </div>

>上传excel文件后,用户将单击下一步按钮
>现在,在此窗口中,将要求用户使用DB字段映射excel字段

过程控制器

 public function actionProcess(){

    $file_name = $_GET['file_name'];
     $id = $_GET['id'];

    try {
        $header_index = $_GET['header_no'];

        $data = \moonland\phpexcel\Excel::widget([
            'mode' => 'import',
            'fileName' => 'uploads/' . $file_name,
            'setFirstRecordAsKeys' => false, // if you want to set the keys of record column with first record, if it not set, the header with use the alphabet column on excel.
            'setIndexSheetByName' => false, // set this if your excel data with multiple worksheet, the index of array will be set with the sheet name. If this not set, the index will use numeric.
            'getOnlySheet' => 0, // you can set this property if you want to get the specified sheet from the excel data with multiple worksheet.

        ]);

        if (isset($data[0])) {
            $headers = $data[0][$header_index];
        } else {
            $headers = $data[$header_index];
        }

    }catch (Exception $x){
        print_r($x->errorInfo);
    }

    return $this->render('excel_options',['headers'=>$headers,'file_name'=>$file_name,'header_index'=>$header_index,'id'=>$id]);

}

Excel选项视图

     <form action="import" method="post">
      <input type="hidden" name="file_name" value="<?=$_GET['file_name']?>" />
            <input type="hidden" name="header_index" value="<?= $_GET['header_no'] ?>"/>
            <input type="hidden" name="model_id" value="<?= $_GET['id'] ?>"/>
            <h1>Maping</h1>

            <div class="row">
                <div class="col-md-2">
                  Ref #:
                </div>
                <div class="col-md-4">
                    <label>
                        <select name="field[0][ref_no]" class="form-control">
                           <option value="">Select A field</option>
                       <?php foreach($headers as $k=>$v) { ?>
                            <?php if (trim($v) != '') { ?>
                           <option value="<?=$k?>"><?=$v?></option>
                                <?php } ?>
                       <?php } ?>
                       </select>
                    </label>
                </div>
            </div>

            <div class="row">
                <div class="col-md-2">
                Meter MSN:
                    </div>
                <div class="col-md-4">
                    <label>
                        <select name="field[0][meter_msn]" class="form-control">
                            <option value="">Select A field</option>
                            <?php foreach ($headers as $k => $v) { ?>
                            <?php if (trim($v) != '') { ?>
                                <option value="<?= $k ?>"><?= $v ?></option>
                                <?php } ?>
                            <?php } ?>
                        </select>
                    </label>
                </div>
            </div>

            <div class="row">
                <div class="col-md-2">
                    Meter Type:
                </div>
                <div class="col-md-4">
                    <label>
                        <select name="field[0][meter_type]" class="form-control">
                            <option value="">Select A field</option>
                            <?php foreach ($headers as $k => $v) { ?>
                                <?php if (trim($v) != '') { ?>
                                    <option value="<?= $k ?>"><?= $v ?></option>
                                <?php } ?>
                            <?php } ?>
                        </select>
                    </label>
                </div>
            </div>

            <div class="row">
                <div class="col-md-2">
                   Sub-Div:
                </div>
                <div class="col-md-4">
                    <label>
                        <select name="field[0][sub_div]" class="form-control">
                            <option value="">Select A field</option>
                            <?php foreach ($headers as $k => $v) { ?>
                                <?php if (trim($v) != '') { ?>
                                    <option value="<?= $k ?>"><?= $v ?></option>
                                <?php } ?>
                            <?php } ?>
                        </select>
                    </label>
                </div>
            </div>


            <div class="row">
                <div class="col-md-2"></div>

                <div class="col-md-4">
                    <br />
                    <input type="submit" class="btn btn-primary pull-left" />

                </div>
            </div>
        </form>

在流程动作控制器中调用上述视图.在这个视图中,我将模型ID作为隐藏字段传递.

>映射文件并单击提交按钮后,文件中的数据将保存到数据库中.下面是将数据保存到数据库中的导入操作

public function actionImport()
{



$file_name = $_POST['file_name'];
$header_index = $_POST['header_index'];
$fieldSet = $_POST['field'];
$model_id = $_POST['model_id'];
print_r($model_id);
die();
.
.
.
return $this->render('excel_finish', ['records_saved' => $ok_count,'status_arr'=>$status_arr]);
}

到目前为止我做了什么

我可以从用户中选择数据(包括excel文件).映射后,它应该转向导入操作,但我得到低于错误

Not Found (#404)
Page not found.

url:http:// localhost:225 / inventory-web / backend / web / meteracceptanceheader / process / import

enter image description here

更新1

在浏览器中检查元素时,我可以看到模型ID.

enter image description here

我一定做错了,我不知道.

任何帮助将受到高度赞赏.

解决方法:

此错误“未找到(#404)页面未找到”.表示它无法从指定的URL解析您的请求.在你的情况下是:

HTTP://本地主机:225 /库存网/后端/网络/ meteracceptanceheader /流程/进口

假设应该是这样的:

HTTP://本地主机:225 /库存网/后端/网络/ meteracceptanceheader /进口

查看本指南,了解如何处理Yii2中的请求,&如何使用“UrlHelper”为您的应用生成网址.

标签:php,yii2,post,yii2-advanced-app,phpexcel
来源: https://codeday.me/bug/20190925/1816508.html