Multipart Image Upload: A Practical Guide for Web, Mobile and Backend
Part 1: Understanding Chunked Multipart Upload
Part 2: Chunking the Image Locally (this post)
Part 3: Building the Multipart Upload API
Part 4: Making Uploads Reliable and Resumable
Part 5: Production Architecture and Cross-Platform Implementation
Before any HTTP request goes out, the client has to turn one file into a list of chunks. This is the most code-heavy part of the series, and the idea is small: one formula, and one operation, reading a range of bytes.
The example
A 5.5 MB image, with a chunk size of 1 MB (1,048,576 bytes):
Part 1 = 1.0 MB (bytes 0 to 1 MB) Part 2 = 1.0 MB Part 3 = 1.0 MB Part 4 = 1.0 MB Part 5 = 1.0 MB Part 6 = 0.5 MB (the remainder)
Enter fullscreen mode
Exit fullscreen mode
The part count comes from:
totalParts = ceil(fileSize / chunkSize)
Enter fullscreen mode
Exit fullscreen mode
Here that's ceil(5.5 / 1) = 6 .β¦