In this episode, we're finally tackling how to handle file chunks on the server side. Up until now, we've just been sending our chunked uploads to a dummy route — but it's time to start doing something real with those chunks.
We start by deciding how to structure our endpoint: should we make a brand new controller just for this, or add a method to our existing Livewire upload component? Either approach is fine, so we go with adding a handleChunk
method in our component and register a new route pointing to it.
After setting up the route and updating our frontend to use it, we jump into the main challenge: receiving and piecing together the chunks. Instead of manually handling the chunk logic (which can be messy and error-prone), we use the handy Laravel chunk upload package. This package simplifies the process by letting us pass a pseudo-file (created in memory from the raw request content) as the upload, and it takes care of merging the chunks behind the scenes.
We walk through how to set up the package, configure it, and pass in the necessary request data. When all the chunks have arrived, it conveniently tells us, giving us a complete uploaded file in Laravel’s usual format so we can do whatever we need with it—store it, move it, process it, you name it.
To wrap up, we test everything with a file upload and confirm it works by checking the logs. At this point, the backend can receive chunked uploads, assemble them, and store the final file. The next challenge? Figuring out how to notify our Livewire component when the upload is done. Stay tuned for that in the next episode!