This episode is for members only

Sign up to access "Build a Livewire Drag & Drop Uploader" right now.

Get started
Already a member? Sign in to continue
Playing
09. Downloading uploaded files

Transcript

00:00
Luckily for us, downloading these files is actually pretty straightforward. And since we've already created a live wire component for uploaded files, we can just create a simple method in here that allows us to download a file by its ID. So we'll just pass the ID of the file model in here and then return the download.
00:18
For now, let's just die dump on the ID just to make sure we hook it up properly. And we're going to come over to the uploaded files component just here. And we're going to set up this anchor to go through and download this. Now to do this, we can get rid of the href or we could keep it in there.
00:34
We're going to say wire click, we're going to prevent the default behavior. And we're going to call that download method on this component, passing through the file ID that we're currently iterating through. So in here, we're just going to output the file ID.
00:47
And that's as easy as it is to hook that in to a method here. So if I click on one of these, sure enough, you see the ID for each of these. Now at the moment, the cursor looks a little bit odd. So I'm actually going to go ahead and add in the href here, not the jref, to a hash.
01:03
And there we go. It works in exactly the same way. So now what we need to do is figure out how to download this file. Well, we know that over in the database, we're storing the file path.
01:11
So we just need to reference that. So if we head over to uploaded files, all we need to do is inside of here, return from using the storage facade, download, which will download a file. And we just want to find the original file path for the ID that we've passed in.
01:28
Now, of course, what you could do to kind of get around this is you could pass the file path directly into here to then do it. But I preferred in the ID. So then we can relook it up just to make sure it does exist,
01:40
just so we're not allowing any path to be downloaded. So we're going to go ahead and say find or fail. Because of course, if that file doesn't exist by that ID, we don't want to do anything. Pass the ID in, grab the file path, and we are done.
01:55
So let's just try this out. Click here and just wait a couple of seconds. There we go. Our file has downloaded.

Episode summary

In this episode, we add downloading functionality for the files our users have uploaded. It turns out this is pretty straightforward, especially since we already have a Livewire component handling our uploaded files.

We start by creating a new method in our component that lets us download a file by its ID. To make sure everything's wired up correctly, we temporarily use a dd() on the file ID when the download action is triggered from the UI. Then, we update our list of uploaded files so each one has an anchor/link that, when clicked, fires off our download method with the right file ID. We play around a bit with the link styling and make sure the browser cursor behaves, so it feels like a regular download link.

Once the click handling is set up, we move on to actually downloading the file. Since we've stored file paths in our database already, all we need is to retrieve the correct file model by ID, double-check it exists, and pass its file path to Laravel's storage download method.

With all of this in place, we try it out in the browser—click a file, and just like that, the file downloads! Super easy and secure, since we’re careful to only allow downloads for IDs that exist.

Episode discussion

No comments, yet. Be the first!