Home » excel » javascript – Laravel ajax file upload (excel file: 1M+ rows)

javascript – Laravel ajax file upload (excel file: 1M+ rows)

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’m trying to create a proper progress bar for my excel file upload in Laravel
I’m using ajax to upload the file and have gotten the progress bar for the upload part to “kind off” work. My only problem, is that when the file is uploaded (100% complete on the upload part) it still says “uploading…”. It looks like uploadProgress is not jumping forward to success when it’s done.

The file is processed in the back by laravel-excel, and it takes a very very long time to process big excel files with 1M+ rows.

Here is also a screenshot of it. I console logged the progress and it all went fine until it should go over to the success function in the ajax call.
enter image description here

What could I do to fix this? To limit the amount of rows in the excel file uploads are not something I can do as the website I’m creating has the need to upload files that has this many or more rows.

Also, my code for the ajax upload:

    // AJAX form upload
    var form = $('.form-import');
    var bar = $('.import-progress-bar');
    var status = $('.import-status');

    form.ajaxForm({
        beforeSend: function() {
            status.empty();
            bar.attr('value', 0);
        },
        uploadProgress: function(event, position, total, percentComplete) {
            bar.attr('value', percentComplete);
            status.html('Uploading...');
            console.log('Upload progress - '+ percentComplete + '%');
        },
        success: function() {
            status.html('Importing rows...');
        },
        complete: function() {
            status.html('Importing complete!');
        }
    });
How to&Answers:

I would use mysql LOAD DATA INTO, to process the excell (csv) file into database. I had a similar task, with several 5-600 MB csv files, which generated php runtime errors.

With the LOAD DATA INTO, I have imported these files in seconds, without issues.

Read more:

https://dev.mysql.com/doc/refman/8.0/en/load-data.html