Resumable file upload
In one of our mobile apps client wants to upload a big size file to a server. The main requirement was to resume upload after connection lost. Also if a connection is poor, it can be interrupted by timeout.
Let’s begin with Apple docs for NSURLSession. It says “Upload tasks send data in the form of a file, and support background uploads while the app is not running“.
That’s it we are looking for! But, if you dive in, you get that in background mode only download task resume is guaranteed. So, after connection lost our upload task start the upload process from the beginning. That’s not what we are really wanted.
After some discussions with a backend team, we consider using this protocol.
Shortly about how it works:
- Take a file chunk equal to chunk size.
- In request, a header sends from to end byte of current chunk and full size of the file.
- Also, send SHA-1 of data in a request header.
- Receive session token and CRC32 of current data of all chunks, that server has already.
- After successful upload check control sums.
We also cache the progress of upload for every file and its additional info: session token, file URL, etc.
The disadvantage is you should implement this on your own. The algorithm is straightforward — recursive call to take the next chunk and checking control sum.