I´m trying to create a progress bar while uploading files and importing data in database.
I have created an importer with laravel and ajax with jquery. In this importer I´m reading all data from a CSV file. I´m getting all rows and cols from this file and while I´m reading this file, I´m doing a insert query with eloquent. This is my code:
$fp = fopen($route, 'r');
// header
$head = fgetcsv($fp, 4096, ';', '"');
$header = [];
$header[0] = 'nomape';
$header[1] = 'direccion';
$header[2] = 'cp';
$header[3] = 'ciudad';
$header[4] = 'provincia';
$header[5] = 'telefono';
$header[6] = 'movil';
$rows = 0;
$cells = 0;
// Rows
while($column = fgetcsv($fp, 8192, ';', '"'))
{
// build array to can combine header and this array
$columns = [];
foreach ($column as $val) {
array_push($columns, $this->delete_accent($val));
}
$column = array_combine($header, $columns);
$result = Listado::insert($column);
if($result){
$rows++;
}
$cells++;
}
fclose($fp);
return 'Importados un total de: ' .$rows. ' de un total de: ' . $cells;
These operations take time. In my web browser console in network tab, I can show my http petition in pending while importing all my data. My CSV has a 69000 register.
With this code I´m building my progress bar:
$.ajax({
xhr: function() {
var xhr = new window.XMLHttpRequest();
xhr.upload.addEventListener("progress", function(evt) {
if (evt.lengthComputable) {
var percentComplete = (evt.loaded / evt.total) * 100;
// Place upload progress bar visibility code here
console.log(percentComplete);
}
}, false);
return xhr;
},
But this returns 100% value in seconds, when these operations take many minutes. My question is how can I get real progress to create my progress bar ?
