简体   繁体   English

如何禁用 Laravel 的输出缓冲

[英]How to disable output buffering for Laravel

I have this function in controller ImportController.php, I seding a file to import using the Laravel Excel library...我在控制器ImportController.php中有这个功能,我使用Laravel Excel库导入一个文件......

  • Laravel version: 5.5 Laravel 版本: 5.5
  • Php Version: PHP 7.2.19-0ubuntu0.18.10.1 (cli) (built: Jun 4 2019 14:46:43) ( NTS ) PHP 版本: PHP 7.2.19-0ubuntu0.18.10.1 (cli) (built: Jun 4 2019 14:46:43) ( NTS )

One sheet the client is sending is taking a lot of time.客户发送的一张纸需要很多时间。 Then I make a progress bar for the client get a feedback when is importing/saving data from the sheet.然后,当从工作表导入/保存数据时,我为客户端制作一个进度条以获得反馈。

  • This is the javascript function here is sending the sheet and token and if is a ajax request.这是这里的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);
        }
  • This is my controller:这是我的控制器:
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());
        }
}

These outputs echo "importing data from sheet" is for testing.这些输出echo "importing data from sheet"用于测试。

I tried with:我试过:

  • ob_flush();
  • flush();
  • php.ini -> output_buffering=Off
  • .htaccess -> mod_env.c -> SetEnv no-gzip 1

But none worked (in laravel).但是没有一个工作(在laravel中)。 In tests outside Laravel ob_flush and flush works fine.在 Laravel 之外的测试中 ob_flush 和 flush 工作正常。

Anyone have any idea?任何人有任何想法?

After a long time and a travel along mountains and 5 cigarettes.经过长时间的山路和5支烟的旅行。

  • In PHP controller function add:在 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);
  • In javascript function to start the XMLHttpRequest:在 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM