This episode is for members only

Sign up to access "Chunking Large Uploads in Inertia" right now.

Get started
Already a member? Sign in to continue
04. Sending chunks to the server


There are a ton of different ways that we can chunk uploads. There are loads of packages that we can use. We can do this manually. But my favorite is Upchunk.
So we're going to get this installed, send the chunks down to our back end. We're going to create a controller to receive them. We're not going to do anything with them just yet.
We'll handle that a little bit later. But let's just get this working so at least it starts to send this data down to the server. So we're going to head over to Upchunk,
and we're going to go ahead and install this, of course, with NPM. This is all on the client side. And to be honest, that's pretty much what we need to do.
Let's go ahead and get this imported and trigger this to actually start to do this. So we're going to go up to the top here, and we're going to import the Create Upload
function from that package. So that is from mux and Upchunk. And then down here, we're just going to invoke this in here. So let's go ahead and invoke Create Upload.
Now, as you'd imagine, this takes a load of different options. This is really important because we need to do a couple of Laravel-specific things.
Behind the scenes, this package does not use something like Axios. So for example, our cross-site request forgery token won't get sent down automatically
with these requests. So let's look at the basics. The first one is going to be the endpoint that we send this down to.
Now, we don't have an endpoint for this just yet. Let's go ahead and just add in an empty string. The second is the method. So by default, this will be put.
But we can go ahead and specify that we want to post this if you need a different route. And then really important is going to be the file, which we know that comes from our state.
So we can just pass that down. Now, the last thing is going to be the chunk size. So you can choose the size of the chunks that you're actually sending down.
And of course, if the file that you're uploading is smaller than this chunk, the whole thing will be sent down in its entirety. So we're going to go ahead and multiply 10 by 1024, which,
of course, is 10 megabytes. But of course, you can go ahead and change this over if you want to and experiment with the sizes. OK, so let's go ahead and build this endpoint out
just so we can see what happens when we actually invoke this. So we don't need to fill anything in the controller just yet. So we're going to make a file store controller.
And if we head over to our routes, let's go ahead and create this out. So I'm just going to copy one of these. And let's go ahead and say post files and file store
controller. And we'll get rid of that method name as well because we're not going to need that. And let's just give this a name.
And we'll just call this files. So let's call that file store. So with this, what we can do, specifically with an inertia, is use the route helper because we have Ziggy set up
within this starter kit. But if you don't, you just want to put the endpoint name in there. OK, let's go over to our file store controller.
And let's just add in an invoke magic method. OK, so if we head over, let's open up our network tab just so we can keep an eye on what's happening. Now, just before we do anything, I've
created out a custom profile in here which will slow this process down. This is going to be important later when we test this. So if you want to add one of these,
go ahead and add this out with the reduced speeds. And we are good to go. We can just select that in here and then see this slightly slow down.
So I'm going to go ahead and choose a file in here. Again, the request is a little bit slow to actually grab the file. Hit Upload.
And as you can see, we get a 419. So that's the one thing that we need to fix to get this working. This is a Cross-Site Request Forgery mismatch exception.
So we need to put this into a header within here directly. So if you are using Inertia, what you can do is come over to your Handler Inertia Requests middleware. And you can just output your Cross-Site Request Forgery
token directly within here. That will be available globally. So let's go ahead and do that. We have a Cross-Site Request Forgery underscore token
function within Laravel, which will give you the token. And then we can pick this up over here and add it to our headers list. So this package allows you to send custom headers down.
So the header for Laravel is Cross-Site Request Forgery token. And then here, we're going to go ahead and pull in the use page directive from, not from view, from Inertia.
And then down here, we can invoke use page. And then we can access any of our props. So we can access props, Cross-Site Request Forgery token.
So now, with each of these chunks being sent down, that will send that Cross-Site Request Forgery header down. And it should work. So let's go over and try this out again.
So let's choose another file here, hit Upload. And as you can see, this file is now being sent to our back end, picked up in that controller in chunks until it completely finishes.
So this will just keep rolling until we get to the last chunk. And then we're pretty much done. Now, obviously, we're not handling
any events or anything yet. We're not even picking this up on the back end. But that is how we trigger the process of setting that, going ahead and kicking off the process
and sending them chunks down. Now, we can kind of improve on this. As part of our overall state, we kind of want to keep this uploaded in there
because we want to be able to access this and compute stuff within this. So I'm going to go ahead and create out an uploader value inside of our state.
And I'm going to assign this in here, just so we have it available and we can do stuff that we need to. So that's pretty much it. That is how we very easily use that package to grab our file,
start to send it down to our back end in chunks. Now, what we need to do is pick this up on the back end, receive each of them chunks, and then store that file once the whole process has been completed.
10 episodes 46 mins


Effortlessly handle large file uploads in your Inertia/Vue apps with chunked, resumable uploading.

We’ll cover the entire upload process for the client and server, display a progress bar, then add the ability to pause, resume and cancel uploads.

From there, you’ll be able to handle huge file uploads anywhere in your applications.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!


No comments, yet. Be the first to leave a comment.