[英]How to disable output buffering for Laravel
我在控制器ImportController.php中有這個功能,我使用Laravel Excel庫導入一個文件......
5.5
PHP 7.2.19-0ubuntu0.18.10.1 (cli) (built: Jun 4 2019 14:46:43) ( NTS )
客戶發送的一張紙需要很多時間。 然后,當從工作表導入/保存數據時,我為客戶端制作一個進度條以獲得反饋。
.....
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 工作正常。
任何人有任何想法?
經過長時間的山路和5支煙的旅行。
$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);
// 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.