如何禁用Laravel的输出缓冲

问题描述 投票:0回答:1

我在控制器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
1个回答
0
投票

经过很长一段时间,沿着山脉和5支香烟旅行。

  • 在PHP控制器功能中添加:
        $response = new StreamedResponse(function() use ($request) {
            for($i = 0; $i <= 3; $i++){
                echo "Data: ".json_encode(['teste' => "teste"])."\n\n";
                flush();
            }
        });
        $response->headers->set('Content-Type', 'text/event-stream');
        $response->headers->set('X-Accel-Buffering', 'no');
        $response->headers->set('Cach-Control', 'no-cache');
        $response->send();
        sleep(5);
  • 在JavaScript函数中启动XMLHttpRequest:
   // Remove the responseType and response as json.

            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.seenBytes = 0;
            xmlHTTP.onreadystatechange = function(evt) {
                console.log("%c Content: ", 'color: red;', evt.srcElement.response);
            };

            xmlHTTP.upload.addEventListener("progress", progressFunction, false);
            xmlHTTP.addEventListener("load", transferCompleteFunction, false);
            xmlHTTP.addEventListener("error", uploadFailed, false);
            xmlHTTP.addEventListener("abort", uploadCanceled, false);
            xmlHTTP.open("POST", _url, true);
            xmlHTTP.send(fd);

© www.soinside.com 2019 - 2024. All rights reserved.