This is the part 1 of 2 in Resumable file upload.

If your application allows users to upload large files. And you are using regular HTML form to post $_FILES. You are doing something wrong.
Imagine browser crashes half due to any reason (e.g. Internet is disrupted). How do you handle that? Imagine the user experience while waiting for the $_FILES being posted to server. What can you do to improve that?
If you have above questions in mind, this tutorial series is for you.
In this series, we will use a wonderful JavaScript library Resumable.js to build a resumable file upload application. This library is designed to introduce fault-tolerance into the upload of large files through HTTP. This is done by splitting each files into small chunks; whenever the upload of a chunk fails, uploading is retried until the procedure completes.

In part 1 of the series, we will go through the steps to implement front-end of the application. Which is building the front-end Resumable.js uploader. And in part 2, we will work on the backend system.

Download link of entire source code is provided in the finale part this series.

Table Of Content

  1. Install Bower
  2. Install Twitter Bootstrap and Resumable.js
  3. Build Resumable.js Uploader
  4. The end
1. Install Bower

Bower is an awesome package management tool, It works by fetching and installing packages from all over, taking care of hunting, finding, downloading, and saving the stuff you’re looking for In this tutorial, we will use Bower to handle assets management (JavaScript and CSS files).

So first thing we need to do is to get Bower installed in our development machine. If you are using Mac OS, simply type following command in the Terminal

Since Bower requires on Node, NPM and Git. In case it does not work above, make sure those are also installed.

Do not be discouraged if your installation does not go through as easy as expected. We highly recommend that you use Bower in your development workflow, it costs your a bit of time to get it installed, it improves productivity a lot more later on.

For detailed installation of Bower, you can refer to its official site.

2. Install Twitter Bootstrap and Resumable.js

Awesome! You have Bower installed, let's see how easy to install Twitter Bootstrap and Resumable.js by using Bower.

Simply type following commands in your terminal from the root of your project folder.

Instead of downloading zip files from those packages' website, extract their source files to the desired location, figure out their dependencies and repeat the same routine to install their dependencies. Bower has done all the work for us.

Check out your project root folder, you should see something similar:

Bower has downloaded all the assets files we need as well its dependency files to the folder bower_components.

3. Build Resumable.js Uploader

We have done all the preparation work, it is time to build the front-end uploader. The reason we call it front-end is because it has the only interface. It does not really work without the backend file handling.

To simulate backend response, we create a PHP file(upload.php) with content below:

This script simulates a forever-green upload. Basically it always tells Resumable.js that the file chunk is uploaded successfully. In part 2 of the series, we will get back to make it really work.

Now let's work on the uploader.

Create a file index.html and copy the content below, we will explain it in detail later on.

  • In <head> part, we include Twitter Bootstrap and Resumable.js asset files and their dependencies.
  • In <div class="container"> ... </div> part, we create a simple and neat uploader interface using Twitter Bootstrap framework.
  • <script> ... </script> part is where we create Resumable uploader. Let's look into this part in details.
  • Line 51-54: instantiate a Resumable instance, specify target to point to upload.php file.
  • Line 56: make #add-file-btn as the browser file button.
  • Line 58-60: when clicking #add-file-btn button, file upload starts.
  • Line 58-60: when clicking #pause-upload-btn button, toggle file upload progress.
  • Line 71: instantiate a ProgressBar instance. ProgressBar is a simple class we create to encapsulate the progress bar.
  • Line 73-88: listen to Resumable.js various events and manipulate progress bar's status accordingly.
  • Line 90-104: ProgressBar class definition. As mentioned early, it provides an easy way to change progress bar's status.

We have built a Resumble.js uploader, if you have followed along correctly, you should be able to see a uploader similar to below when accessing the index.html page:

4. The end

Now we have completed the first tutorial. Next tutorial, we are going to build backend part of the uploader.
Make sure to sign up our newsletter, so that we can update you once next tutorial is out.

Hopefully this simple tutorial helped you with your development.
If you like our post, please follow us on Twitter and help spread the word. We need your support to continue.
If you have questions or find our mistakes in above tutorial, do leave a comment below to let us know.