Large File Resume Upload
Resume a large upload after a drop by tracking confirmed ranges, re-requesting only the missing part, and verifying the assembled file with a checksum.
On this page
Introduction
This guide covers resuming large uploads after a network drop, a recurring concern in the file-upload scenario. A two-hour upload that fails at minute 90 should not restart from zero. Tracking which ranges the server has confirmed, re-requesting only the missing part, and verifying the assembled file with a checksum turns a fatal drop into a short retry, which is the difference between a usable and an unusable large-file feature.
Track confirmed ranges
Record the byte ranges the server has received so a reconnect can ask for the gaps, which is the state that makes resume possible at all.
The trade-off worth naming is that record the byte ranges the server has received so a reconnect can ask for the gaps, which is the state that makes resume possible at all adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
app.get('/upload/:id/status', async (req, res) => {
const ranges = await getRanges(req.params.id);
res.json({ size: await getSize(req.params.id), ranges });
});
Upload the missing part
On reconnect, compute the missing ranges and upload only those, so a drop at 90 percent costs 10 percent of the bandwidth, not 100.
The trade-off worth naming is that on reconnect, compute the missing ranges and upload only those, so a drop at 90 percent costs 10 percent of the bandwidth, not 100 adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
async function resume(id, file) {
const { size, ranges } = await fetch('/upload/' + id + '/status').then(r => r.json());
for (const [s, e] of missing(ranges, size)) {
await uploadRange(id, s, file.slice(s, e));
}
}
Verify the assembly
Send a full-file checksum and have the server compare it to the assembled object, so a dropped or duplicated range is caught before the upload is marked complete.
The trade-off worth naming is that send a full-file checksum and have the server compare it to the assembled object, so a dropped or duplicated range is caught before the upload is marked complete adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Idempotent chunks
Make each range upload idempotent by keying on offset, so a retry that lands twice does not corrupt the assembly, which is the subtle bug in naive resume.
The trade-off worth naming is that make each range upload idempotent by keying on offset, so a retry that lands twice does not corrupt the assembly, which is the subtle bug in naive resume adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Test a mid-drop
Drop at 50 percent, reconnect, and assert the file assembles and matches the original checksum, so resume is verified end to end.
The trade-off worth naming is that drop at 50 percent, reconnect, and assert the file assembles and matches the original checksum, so resume is verified end to end adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Sources
Sources: HTTP Range, tus protocol.
FAQ
- Range-based or chunk-based resume?
- Chunk-based is simpler to reason about because each chunk is an atomic ack; range-based saves bytes when chunks are tiny but adds bookkeeping for partial chunks.
- How do I verify the assembled file?
- Send a full-file checksum at the end and have the server compare it to the assembled object, so a dropped or duplicated chunk is caught before the upload is marked done.
Report a problem with this articleOpens a pre-filled issue on GitHub.