Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel uploading files asynchronously

I'm fairly familiar with Laravel Queues and running asynchronous logic in Laravel by scheduling jobs.

The website I am building needs to handle large video files, so I need these uploads to be async, users now are waiting 5 to 10 minutes for their file to upload.

Is this possible? How would this be implemented into a Laravel Queue? What gets sent to the server in the request?

I can't find anything on Google tackling this issue, so I'm just looking for some general guidance.

The ideal would be for the user to choose their file, click upload, then a job is scheduled to upload the file and notify the user by email when complete.

Thanks!

like image 204
Luke Brandon Farrell Avatar asked Jan 24 '26 12:01

Luke Brandon Farrell


1 Answers

For files to upload, the page that initiated the upload must remain open for the duration of the upload. This makes sense in itself because otherwise if a user chooses to close the page with the upload in progress, you could essentially "steal" the uploading file regardless of the user's action.

The only practical approach here is to allow the user to select files and upload them using an XHR request, something along the lines of:

var payload = new FormData();
payload.append('file', document.getElementById('fileInput'));

var xhr = new XMLHttpRequest();
xhr.open('POST', '/upload', true);

xhr.addEventListener('load', function (e) {
    // Do stuff when upload is complete
});

xhr.send(payload);

Uploading the files asynchronously, allows the user to do other action within the same page while waiting for the upload to finish (e.g. fill in additional data required with the upload).

like image 117
Bogdan Avatar answered Jan 27 '26 03:01

Bogdan