In this episode, we build on our file upload implementation by adding the ability to pause and resume uploads. If you've ever started a big upload and worried you'd need to stop halfway, these new features have got your back!
We start by updating our UI to add two buttons for each uploading file: one to pause and one to resume (these swap based on the file's current state). These buttons trigger events—when you click pause, we emit a pause
event with the corresponding file's ID, and likewise for resume.
Instead of keeping extra state around to track which files are paused, we cleverly use the paused
property built into our file upload (upchunk) instance. This keeps our code cleaner. Depending on that property, we show the right button at the right time.
Next up, we add some simple functions to our dashboard for actually pausing and resuming uploads. Each function looks up the correct file, and then calls either the pause
or resume
method from upchunk.
Finally, we test it out with a larger file. We can pause whenever we want, see that the upload really stops, then hit resume and watch it pick right back up. You can pause and resume as many times as you want, making for a really nice user experience!
So, with just a few tweaks, we now have full control over our file uploads—thanks to those handy upchunk methods.