我正在研究Yii2
。我正在导入Excel文件。导入时,我还发送了先前模型的id
。该过程如下
创建控制器
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文件后,用户将单击“下一步”按钮
过程控制器
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>
以上视图在process
动作控制器中调用。在此视图中,我将模型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文件)。映射后,它应该转到导入操作,但是我遇到了错误
未找到(#404) 找不到页面。
URL: http://localhost:225/inventory-web/backend/web/meteracceptanceheader/process/import
更新1
在浏览器中检查元素时,我可以看到model id
。
我一定在做我不知道的错误。
任何帮助将不胜感激。
答案 0 :(得分:1)
此错误“未找到(#404)页面。”意味着它无法解析您从指定URL发出的请求。您的情况是:
http://localhost:225/inventory-web/backend/web/meteracceptanceheader/process/import
假设应该是这样的:
http://localhost:225/inventory-web/backend/web/meteracceptanceheader/import
查看本指南,了解如何在Yii2中处理请求,以及如何使用“ UrlHelper”为您的应用生成Urls。
答案 1 :(得分:0)
您确定通过帖子$file_name = $_POST['file_name']
获得文件名吗?
只需尝试var_dump($_FILE)
变量。
使用此文件,您将获得文件名$_FILES["file_name"]["name"]
。