简体   繁体   中英

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...

  • Laravel version: 5.5
  • Php Version: 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.
.....
            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.

I tried with:

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

But none worked (in laravel). In tests outside Laravel ob_flush and flush works fine.

Anyone have any idea?

After a long time and a travel along mountains and 5 cigarettes.

  • In PHP controller function add:
        $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:
   // 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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