This episode is for members only

Sign up to access "Multiple Drag And Drop File Uploading and Processing with Inertia" right now.

Get started
Already a member? Sign in to continue
09. Cancelling uploads


So up chunk gives us a really convenient way
to abort a complete upload. So that would just stop the chunks uploading and cancel off any further requests to the file. But because we are also creating the file in the database
while the upload is happening, we also need to look at deleting the file from the database as well, which is pretty straightforward if you've worked with Inertia before.
But let's work through both these things that we need to do. Now, the first thing that we're going to do is create an upload item component.
And we're just going to create out a bunch of buttons under here, which we'll use to control our upload. That'll include pausing and resuming later. OK, so we'll go ahead and say flex item center
and set a space on the x-axis of 3. And we only want to show these buttons when this upload is actually uploading. So let's go ahead and do a VIF in there.
And in here, we can just create out a bunch of buttons. So this is going to be the Cancel Upload button. Let's go ahead and start this up really quickly. So we'll set a text of blue 500 here.
And we'll set the text to small. And we'll set a font of medium. OK, let's just check that that looks OK. And then we'll fill in the functionality.
There we go. So when we click that, we want to cancel the upload. OK, so in here, we're going to emit an event from this. Because we don't really want to do this
at the individual component level, because we've got a higher up list of uploads. We want to remove the upload from that list. So we want to emit an event from this component to say,
please cancel this specific upload by its ID. To do that, we're going to head up to the top of the file here. And we're going to say Emit and Define Emits. That is a view-specific functionality.
And it just means that we've got an Emit function now that we can trigger to emit something from this component. So down here under Cancel Upload, we can just say V on Click.
And we can Emit, Cancel from here. And we need to know which one we're canceling. So we can just pass in the upload ID. So let's handle this cancel event
that we are emitting now. And we'll do that over in our dashboard where we have all of our upload stuff. So inside of here, we want to come down
to our actual component that we're iterating through. Let's pull these down line by line just so things are a little bit tidier. And then in here, we're going to say V on Cancel.
And we're going to call a function here called Cancel Upload. So we can go right to the top now. And let's just do this directly under here, Cancel Upload.
And we know that because we're passing that ID here, we can receive that ID into there. So let's just console log on that ID, make sure we've got everything hooked up properly.
And then we can actually cancel this off. So let's go ahead and upload a file, head over to our console, hit Cancel, and there we go. Sure enough, that's the upload that we want to cancel.
So the first thing that we can do, and the easiest thing to do, is get an upload by its ID, which we know we can now use ID for. Then go into the file, which is the UpChunk uploader,
and we can just call the abort method. So that will abort the file actually uploading. So very, very simple to do that with UpChunk, which is now within this file.
Hit Abort, and that will stop the upload. So let's go over and try this out. We'll head over to our Network tab, and let's just choose any of these files,
hit Cancel Upload, and there we go. That is completely aborted. No more requests are being sent down, and no more chunks are being sent down.
Okay, now we want to delete the file from the database, because we really don't need this hanging around in the database. If it's been canceled, there's no point,
because there's not going to be a file attached to this. So down here, we're going to remove this from our backend. So let's go and create out a controller to handle this, and we'll call this Video Destroy Controller.
And let's head over to our Roots here, create out a route for this. So that's going to be a Delete Request to Videos Video, Video Destroy Controller,
and we'll call this Videos and Destroy. Now, really importantly at the moment, we are not authorizing any of these requests. We'll come back to that a little bit later,
but it's super important that we do eventually authorize these, otherwise anyone can just hit these routes and delete any of our videos.
So let's go ahead and say Invoke, and this is where we want to delete that video. So we can pretty much fill this in, because it's pretty straightforward.
We're just going to say Video Delete, and then we're going to return back. We can use an inertia request for this as well. So let's go ahead and pull that video in.
Remember, really crucially, we need to authorize this later. Okay, so now that we've got that Videos Destroy in there, we can just send a request using Inertia's router. So let's just make sure we've got this pulled in
at the top, and we do, and let's go ahead and send a Delete Request down to Route, Videos Destroy, and of course, as part of this route buildup,
we need to pass the ID into there. Now already, we need to make sure that we are passing down Preserve Scroll, and really importantly, Preserve State,
so all of the state is preserved on our page once we come back from inertia. So let's try this out. I'm going to go over to the database,
clear everything out of here. We'll go over to here. We'll start an upload. We'll delete it.
So let's go and cancel the upload, and there we go. So we've got a request sent down there. We shouldn't have anything in our database now. That has been completely deleted,
but of course, this is still hanging around on the client side, so we need to remove this from the list. So if we head back over here on the success of this being deleted,
so we can hook into inertia's events here when we send a request down, we just want to go ahead and set the uploads value to a filtered version, not including that.
We could extract this out to another function, but we're not going to be using this anywhere else, so we might as well just do it directly in here. So we're going to go ahead and say uploads value.
We're going to reassign that to a filtered version of that. Now to filter this, we'll get the upload through, and we just want to say we'll return all uploads where the upload ID doesn't equal the ID
that we've passed into here. So we'll just return all of the others except the one that we're deleting. Okay, let's try this out.
So let's go over to the database, make sure that's clear, and we'll go ahead and upload a file. We'll hit cancel. That's been canceled off in terms of the request,
deleted from the database, and of course, removed from the list on the client side as well. So there we go. That is deleting our files.
17 episodes1 hr 56 mins


Build a multiple file chunked uploader in Inertia with the ability to pause, resume and cancel uploads. We’ll also be able to edit metadata (like the title and description) for each upload — even while they’re uploading and processing.

In the second half of the course, we’ll learn how to queue and process uploaded files on the backend (like encoding videos), and in realtime, report back to the client with progress updates using WebSockets.

It’s everything you need to know to upload and process large files in Inertia.

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


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