我有一个使用 Yii2 基本框架的应用程序,我正在使用 Kartik InputFile 上传文件。文件成功上传到系统,页面也成功跳转到下一页,但是页面跳转到下一页前几秒,进度条显示如下错误
尽管文件已成功上传,为什么此小部件显示通知“上传失败”?
这是我在 _form.php 中的代码:
<?php $form = ActiveForm::begin(); ?>
<?php
echo FileInput::widget([
'name' => 'file_uploads',
'language' => 'id',
'options' => [
'accept' => 'doc/*', 'file/*',
'multiple' => true,
'enableLabel' => true,
],
'pluginOptions' => [
'allowedFileExtensions' => ['xls', 'xlsx'],
'uploadUrl' => Url::to(['/pib/pib-data/create']),
'maxFileSize' => 1024,
'maxFileCount' => 1,
'showCaption' => false,
'showMessage'=>false,
'purifyHtml'=>false
]
]);
?>
<?php ActiveForm::end(); ?>
这是我在控制器中使用的代码:
<?php
public function actionCreate() {
$model = new PibData();
$connection = \Yii::$app->db;
$userId = Yii::$app->user->id;
if (Yii::$app->user->isGuest) {
return $this->redirect(['/site/login']);
}
$sqlHeader = "UPDATE pib_data SET is_exported = '1' WHERE user_id = '$userId'";
$exeQuery = Yii::$app->db->createCommand($sqlHeader)->execute();
$date = new DateTime('now', new \DateTimeZone('Asia/Jakarta'));
$created_at = $date->format('Y:m:d H:i:s');
$dirTrash = Yii::getAlias('@webroot/trash');
$dirSubTrash = Yii::getAlias('@webroot/trash/trash_pib');
if (!is_dir($dirTrash)) {
mkdir(Yii::getAlias('@webroot/trash'));
if (!is_dir($dirSubTrash)) {
mkdir(Yii::getAlias('@webroot/trash/trash_pib'));
}
} else {
if (!is_dir($dirSubTrash)) {
mkdir(Yii::getAlias('@webroot/trash/trash_pib'));
}
}
if (Yii::$app->request->post()) {
$file = UploadedFile::getInstancesByName('file_uploads');
$middleName = substr(md5(microtime() * 100000), rand(0, 9), 5);
if ($file !== null) {
$name = $userId . '_' . $middleName . '_' . date('Y-m-d') . '_' . $file[0]->getBaseName() . "." . $file[0]->getExtension();
$pathproduct = Yii::getAlias('@webroot/trash/trash_pib/') . $name;
$file[0]->saveAs($pathproduct);
Yii::$app->response->format = Response::FORMAT_JSON;
return [];
} else {
$error[] = "Silahkan pilih terlebih dahulu file" . "<strong>" . " Excel " . "</strong>" . "yang akan di convert.";
Yii::$app->session->setFlash('error', $error);
return $this->render('create', ['model' => $model]);
}
$modelUser = Users::find()->where(['id' => $userId])->one();
$parentId = $modelUser->parent_id;
$objPHPExcel = new \PHPExcel();
$fileName = Yii::getAlias('@webroot/trash/trash_pib/') . $name;
$inputFiles = fopen(Yii::getAlias('@webroot/trash/trash_pib/') . $name, "r");
try {
$inputFileType = \PHPExcel_IOFactory::identify($fileName);
$objReader = \PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($fileName);
} catch (Exception $ex) {
die('Error');
}
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestDataRow();
$highestColumn = $sheet->getHighestDataColumn();
$colNumber = PHPExcel_Cell::columnIndexFromString($highestColumn);
$col = $colNumber - 1;
$arrayData = [];
for ($row = 1; $row <= $highestRow; ++$row) {
$rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);
$arrayData[] = array_map(function($values) {
$tempArray = [];
foreach ($values as $key => $value) {
$newKey = $key + 1;
$tempArray[] = $newKey . '_' . $value;
}
return $tempArray;
}, $rowData);
}
$arrayHeader = array_shift($arrayData);
$countArrayData = count($arrayData);
$countArrayHeader = count($arrayHeader[0]);
for ($i = 0; $i < $countArrayData; $i++) {
for ($j = 0; $j < $countArrayHeader; $j++) {
if ($j < 9) {
$finalData[$i][] = substr($arrayData[$i][0][$j], 2);
} else {
$finalData[$i][] = substr($arrayData[$i][0][$j], 3);
}
}
}
for ($x = 0; $x < $countArrayData; $x++) {
array_unshift($finalData[$x], "dummy");
}
$tempData = mysql_escape_string(json_encode($finalData));
$tempHeader = json_encode($arrayHeader);
$header = base64_encode($tempHeader);
$command_1 = "DELETE FROM pib_json_data WHERE user_id= '$userId'";
$query_1 = Yii::$app->db->createCommand($command_1)->execute();
$command_2 = "INSERT INTO pib_json_data(json_data, user_id, created_at) VALUES('$tempData', '$userId', '$created_at')";
$query_2 = Yii::$app->db->createCommand($command_2)->execute();
$_SESSION['header'] = $header;
$_SESSION['validHeader'] = 'validHeader';
return Yii::$app->response->redirect(['/pib/pib-header/select-header']);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
?>
anyhelp 将不胜感激。
谢谢