In this episode, we're diving into how to handle file uploads that come in "chunks" on the server—think big video files that need to be uploaded in smaller bits. We start by looking into our video file store controller, where each chunk for a file is sent, and talk about how, even though there are a lot of chunks, we're really just dealing with one file at a time thanks to how the controller is set up.
We go over how the client is sending these chunks by checking the network tab in the browser and notice that each chunk arrives as the request body and includes handy headers like Content-Range
and Content-Length
. These help us know where each chunk fits in the overall file.
Rather than reinventing the wheel, we use the Laravel chunk uploads package, which is well-suited for this job. After installing it, we set up a file receiver that can take in chunks using Laravel's UploadedFile tools. This involves reading the content from the request body and faking an uploaded file so we can work with it just like a standard file upload in Laravel.
The package handles piecing the chunks together as they arrive, and we add a check to see when the upload is complete. Once the last chunk is in, we move the file to our storage directory and update the corresponding video record in the database with the new file path.
Finally, we test everything, confirm the chunked upload works, the file gets stored correctly, and the record in the database is updated. For now, the file has no extension, but we'll handle video encoding and proper naming later. That's the full round-trip for chunked uploads using server-side handling!