Resumable Large Files
With UploadType, large files travel in chunks, two at a time. Pause the transfer, or reload the
page halfway: selecting the same file again sends only the missing chunks.
Chunks per uploader
Files above chunk_size travel in chunks, parallel_chunks at a time. A failed
chunk is retried up to three times before the file is marked as failed.
The defaults are 5 MB and 3. This public server accepts 1 MB at most and keeps no file, so this uploader cuts the archive into 16 chunks of 64 KB.
20 large:
21 max_size: 1M
22 allowed_types: ['application/zip']
23 chunk_size: 64K
24 parallel_chunks: 2
Nothing to add in the form
The field picks the transport from the file size: one request for a small file, chunks for a large one. Pause and resume buttons appear on chunked transfers.
Each chunk is stored as soon as it arrives. The browser keeps a signed resume token in IndexedDB: after a reload, select the same file, the server lists the chunks it already holds, and only the missing ones are sent.
27$builder->add('file', UploadType::class, [
28 'uploader' => 'large',
29 'layout' => 'dropzone',
30 'help_text' => 'Up to 1 MB, sent in 64 KB chunks',
31]);
Listen to the field
The field emits browser events while it works: start when a transfer begins,
chunk each time the server stores one, complete when it holds the whole file.
The counter under the field is this Stimulus controller, bound to these events
with data-action. The upload does not need it: the field shows its own
progress. See the JavaScript API.
18 start() {
19 this.counter.reset();
20 this.sentTarget.textContent = 'No chunk sent yet.';
21 this.summaryTarget.hidden = true;
22 }
23
24 chunk(event) {
25 this.counter.record(event.detail.totalChunks);
26 this.sentTarget.textContent = this.counter.progressLabel();
27 }
28
29 complete() {
30 const label = this.counter.summaryLabel();
31 if (null === label) return;
32
33 this.summaryTarget.hidden = false;
34 this.summaryTarget.textContent = label;
35 }
Slowed down on purpose
A server stores 16 small chunks in well under a second: too fast to pause. This listener holds each chunk of the generated archive for 700 ms.
Two chunks wait at most, for the whole site. Your own files are never slowed down.
54#[AsEventListener(event: KernelEvents::REQUEST)]
55public function onRequest(RequestEvent $event): void
56{
57 $request = $event->getRequest();
58 if (!$event->isMainRequest() || 'ux_upload_handle' !== $request->attributes->get('_route') || !$request->isMethod('PUT')) {
59 return;
60 }
61
62 $uploadId = $request->attributes->get('uploadId');
63 if (!\is_string($uploadId) || !$this->cache->hasItem($this->key($uploadId))) {
64 return;
65 }
66
67 // A waiting chunk holds a PHP worker. Two wait at most, for the whole site:
68 // with more visitors on the demo, their chunks are simply not slowed down.
69 foreach ([0, 1] as $slot) {
70 $lock = $this->locks->createLock(self::SLOT.$slot, 5);
71 if ($lock->acquire()) {
72 $this->clock->sleep(self::DELAY_SECONDS);
73 $lock->release();
74
75 return;
76 }
77 }
78}