i have two ajax calls. the first call is for read a file and save it into the DB (mysql) and at the bottom of a for loop they set an session variable of the "status" like the interval. The second call return the session variable.
this is my javascript code:
var interval = null;
function test(data) {
var i = 0;
interval = setInterval(function () {
$.ajax({
url: '/admin/movies/progress',
type: "GET",
async: true,
dataType: "text",
success: function (data) {
console.log(data);
$('#saveFileProgressBar').width(data[0]);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
toastr.error('error progressbar', 'Download File');
}
});
i++;
if(i == 5){
clearInterval(interval);
}
}, 500);
$.ajax({
url: '/admin/movies/1',
type: "GET",
async: true,
dataType: "text",
success: function (data) {
console.log(data);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
toastr.error('error', 'Download File');
}
});
}
this is my laravel 5.4 code:
// Mapped to mysite.com/admin/movies/progress
public function getProgress() {
return Response::json(array(Session::get('progress')));
}
// Mapped to mysite.com/admin/movies/1
public function postGenerate() {
// getting values from form (like $record_num)
Session::put('progress', 0);
Session::save(); // Remember to call save()
for ($i = 1; $i < 100; $i++) {
sleep(1);
Session::put('progress', $i);
Session::save(); // Remember to call save()
}
return "done";
}
///////EDIT///////
my new PHP code:
public function getProgress() {
$rawData = file_get_contents('plugins/elFinder-2.1.25/files/data/progressFile.json');
$cacheData = json_decode($rawData, true);
return $cacheData;
}
public function postGenerate() {
// getting values from form (like $record_num)
for ($i = 0; $i < 10; $i++) {
$data['progress'] = $i;
$fres = fopen('plugins/elFinder-2.1.25/files/data/progressFile.json', 'w');
fwrite($fres, json_encode($data));
fclose($fres);
sleep(1);
}
return "true";
}
my javascript code:
var interval = null;
function test() {
var i = 0;
interval = setInterval(function () {
$.ajax({
url: '/admin/movies/progress',
type: "GET",
async: true,
dataType: "text",
success: function (data) {
console.log(data);
$('#saveFileProgressBar').width(data.progress);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
toastr.error('Es ist ein Fehler aufgetreten beim Auslesen der Datei', 'Download File');
}
});
i++;
if(i == 5){
clearInterval(interval);
}
}, 500);
$.ajax({
url: '/admin/movies/1',
type: "GET",
async: true,
dataType: "text",
success: function (data) {
console.log(data);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
toastr.error('Es ist ein Fehler aufgetreten beim Auslesen der Datei', 'Download File');
}
});
}