【发布时间】:2020-01-28 19:26:06
【问题描述】:
我在控制器 ImportController.php 中有这个功能,我使用 Laravel Excel 库创建一个要导入的文件...
- Laravel 版本:
5.5 - PHP 版本:
PHP 7.2.19-0ubuntu0.18.10.1 (cli) (built: Jun 4 2019 14:46:43) ( NTS )
客户发送的一张纸需要很长时间。然后我为客户制作一个进度条,以便在从工作表中导入/保存数据时获得反馈。
- 这是这里的 javascript 函数,用于发送工作表和令牌,如果是 ajax 请求。
.....
let fd = new FormData();
fd.append(_archiveInputId, archivoSeleccionado.files[0]);
fd.append("_token", window.Laravel.csrfToken);
fd.append("isHttpRequest", true);
let xmlHTTP = new XMLHttpRequest();
xmlHTTP.upload.addEventListener("progress", progressFunction, false);
xmlHTTP.addEventListener("load", transferCompleteFunction, false);
xmlHTTP.addEventListener("error", uploadFailed, false);
xmlHTTP.addEventListener("abort", uploadCanceled, false);
xmlHTTP.responseType = 'json';
xmlHTTP.response = 'json';
xmlHTTP.open("POST", _url, true);
xmlHTTP.send(fd);
.....
/**
* Progress Function
* @param evt
*/
function progressFunction(evt) {
console.log(evt);
}
- 这是我的控制器:
public function import(ImportFormRequest $request)
{
$isHttpRequest = $request->has('isHttpRequest') ? $request->has('isHttpRequest') : false;
if($isHttpRequest){
echo "creating EventsImport class";
flush();
sleep(1);
}
$import = new EventsImport(EventsImport::TYPE_SIMPLE, $request->file('sheet'));
try{
if($isHttpRequest){
echo "importing data from sheet";
flush();
sleep(1);
}
Excel::import($import, $request->file('sheet'));
}catch (\Exception $e){
return $this->returnJsonResponseHttpRequest("nok","Erro ao realizar importação! Erro: ".$e->getMessage());
}
}
这些输出echo "importing data from sheet" 用于测试。
我试过了:
ob_flush();flush();php.ini -> output_buffering=Off.htaccess -> mod_env.c -> SetEnv no-gzip 1
但没有一个工作(在 laravel 中)。在 Laravel 之外的测试中 ob_flush 和 flush 工作正常。
有人知道吗?
【问题讨论】:
标签: php laravel apache output-buffering