Infinite | Squares

Art of code and more

Resumable Download and Upload With Qt

Qt is one of my favorite C++ framework. It provides great IDE, GUI components that easy to use and its signal slot mechanism is powerful. I did a lot of work with Qt a while back and wrote some Qt apps for Symbian and Meego (It’s unfortunate that both platform is dead now). I can say that coding with Qt is fun.

One of the project that I did required a resumable download and upload. How we can have a a resumable download and upload client?

Resumable download

The code is available here

The key part in resumable download is HTTP header Range. Range header allow us to request only part of an entity, starting from specified bytes. Example of Range header.

Range: bytes=500-999

When we received bytes of the file, we store those bytes into a file. If connection stopped and we want to resume, we simply get the size of downloaded part and continue from there by specifying the start bytes. See here for the relevant part of the code.

Resumable upload

The code is available here

To enable resumable upload we have to use new approach on the server side too. Simply user standard HTTP POST with Content-Type: multipart/form-data just doesn’t cut it. One protocol that specify how we can create reliable and resumable file upload is tus. The tl;dr is

  • POST to create new file with file size included in header, server will create the file id and sent it back
  • HEAD to get current file size, server will return offset value on response header
  • PATCH to send file data start from offset value. Client will upload file in several chunks.

Both in one application

You can see both download and upload plugin integrated in one sample application here. Note that to test resumable upload with tus, you should prepare you own tus server. You can choose several tus server implementation from here