Add new comment

Uploading big files to Drupal 8: PLUpload integration

Difficulty: 
Piece of Cake

As of this writing the only site building readily available module is the PLUpload file widget.

This module depends on the PLUpload form element provided by the Plupload integration module, that is a more developer oriented module.

With the Plupload widget/integration you can:

  • Have a drop-in replacement for core's file widget that supports big uploads (HTML 5 based)
  • Have a powerful FAPI element to upload big files

What kind of things can you do with this? See this video from our "Powerpoint to Files" post where a user uploads a several hundred megabytes Powerpoint file that gets converted to high quality images on the server side:

 

Once you have deployed the Plupload integration module you can use the FAPI widget to add uploading capabilities with code like this:

  /**
   * {@inheritdoc}
   */
  public function form(array $form, FormStateInterface $form_state) {
    $form = parent::form($form, $form_state);
    $form = Form::Instance('', $form);

    $form->AddElement('slides', [
      '#type' => 'plupload',
      '#title' => t('Cargar archivos'),
      '#autoupload' => TRUE,
      '#autosubmit' => TRUE,
      '#description' => t('Suba aquí los archivos de slides que se adjuntaran a la presentación.'),
      '#submit_element' => '#slides-upload',
      '#upload_validators' => [
        'file_validate_extensions' => ['zip ppt pptx'],
      ],
      '#plupload_settings' => [
        'runtimes' => 'html5,html4',
        'unique_names' => TRUE,
        'max_file_size' => static::MAX_FILESIZE_UPLOAD,
        'chunk_size' => '1mb',
      ],
    ]);

    $form = $form->Render();

    return $form;
  }

To use the site building capabilities of the Plupload File widget you simply pick the Plupload widget:

After that you will be presented with a Plupload widget to upload very big files comfortably;

What is important about this module is the fact that it is an extremely simple drop-in replacement for the core File widget. Indeed, most of the logic is inherited from the core File widget, making it future proof with very little maintenance.