File upload MDB Pro component

File upload - Bootstrap 5 & Material Design 2.0 plugin

MD Bootstrap's File Upload plugin is an extension that allows you to upload files by using drag and drop functionality. Easy to use, setup and customize.

Note: Read the API tab to find all available options and advanced customization


Basic example

For each input with a data-file-upload="file-upload" attribute you have to add a wrapper with the class file-upload-wrapper.


          <div class="file-upload-wrapper">
            <input
              type="file"
              id="input-file-now"
              class="file-upload-input"
              data-file-upload="file-upload"
            />
          </div>
        

Default message example

By adding data-default-msg attribute you can set main message of the file upload.


          <div id="dnd-custom-default-msg" class="file-upload-wrapper">
            <input
              type="file"
              class="file-upload-input"
              data-default-msg="custom message"
              data-file-upload="file-upload"
            />
          </div>
        

Custom height example

By adding data-height attribute you can set its custom height.


          <div id="dnd-custom-height" class="file-upload-wrapper">
            <input
              type="file"
              class="file-upload-input"
              data-height="500"
              data-file-upload="file-upload"
            />
          </div>
        

Max size

By adding data-max-file-size attribute you can set max size of a file.


          <div id="dnd-max-size" class="file-upload-wrapper">
            <input
              type="file"
              class="file-upload-input"
              data-max-file-size="2M"
              data-file-upload="file-upload"
            />
          </div>
        

Default value

By adding data-default-value attribute you can set default file in the file upload element.


          <div id="dnd-default-value" class="file-upload-wrapper">
            <input
              type="file"
              class="file-upload-input"
              data-default-file="https://mdbootstrap.com/img/Photos/Others/images/89.jpg"
              data-file-upload="file-upload"
            />
          </div>
        

Disable

By adding data-disabled attribute you can disable the component.


          <div id="dnd-disable" class="file-upload-wrapper">
            <input
              type="file"
              class="file-upload-input"
              data-disabled="disabled"
              data-file-upload="file-upload"
            />
          </div>
        

Accept formats

By adding data-accepted-formats you can set allowed file types.


          <div id="dnd-accept-formats" class="file-upload-wrapper">
            <input
              type="file"
              class="file-upload-input"
              data-accepted-extensions="image/*, .pdf"
              data-file-upload="file-upload"
            />
          </div>
        

Disabled remove button

By adding data-disabled-remove-btn attribute you can remove "Remove button".


          <div id="dnd-desabled-btn" class="file-upload-wrapper">
            <input
              type="file"
              class="file-upload-input"
              data-disabled-remove-btn="true"
              data-file-upload="file-upload"
            />
          </div>
        

Multiple files

By adding data-multiple attribute you can allow to upload more than single file.


          <div class="file-upload-wrapper">
            <input
              id="dnd-multiple-files"
              type="file"
              class="file-upload-input"
              data-multiple="true"
              data-file-upload="file-upload"
            />
          </div>
        

Multiple with files limit

By adding data-max-file-quantity attribute you can set limit of uploaded files.


          <div class="file-upload-wrapper">
            <input
              id="dnd-multiple-with-file-limit"
              type="file"
              class="file-upload-input"
              data-multiple="true"
              data-max-file-quantity="3"
              data-file-upload="file-upload"
            />
          </div>
        

File upload - API


Usage

Via data attributes


        <div id="dnd" class="file-upload-wrapper">
          <input
            id="file-upload"
            type="file"
            data-file-upload="file-upload"
            class="file-upload-input"
            data-main-error="Ooops, error here"
            data-format-error="Bad file format (correct format ~~~)"
          />
        </div>
      

Via JavaScript


        FileUpload.getInstance(document.getElementById('file-upload')).update({
          previewMsg: ..., 
          maxSizeError: ...
        });
      

Via jQuery

Note: By default, MDB does not include jQuery and you have to add it to the project on your own.


        $('.example-class').fileUpload();
      

Options

Name Type Default Description
acceptedExtensions Array '[]' Allows you to set specific file formats
defaultFile null / string 'null' Allows you set default file
defaultMsg string 'Default message' Changes text of default message
disabled string / boolean 'false' Makes drag and drop disabled
disabledRemoveBtn boolean 'false' Allows you to disabled remove button
formatError string 'Your file has incorrect file format (correct format(s) ~~~)' Changes text of format's error (add '~~~' to show allowed formats)
height null / number 'null' Changes height of dropzone
mainError string 'Ooops, something wrong happended.' Changes text of main error message
maxFileQuantity number 'Infinity' Allows you to upload specific number of files
maxFileSize Infinity / number 'Infinity' Changes allowed file max size
maxSizeError string 'Your file is too big (Max size ~~~)' Changes text of size's error (add '~~~' to show value of max size)
multiple boolean 'false' Allows you to upload more than single file
previewMsg string 'Drag and drop or click to replace' Changes text of file's preview
removeBtn string 'Remove' Changes text of remove button

Methods

Name Description Example
dispose Manually removes a drag and drop FileUpload.dispose()
update Manually updates settings in drag and drop FileUpload.update()

For example, to update component


        const fileUpload = document.getElementById('file-upload');
        const fileUploadInstance = FileUpload.getInstance(fileUpload);
        fileUploadInstance.update({ multiple: true, });
      

Events

Name Description
error.mdb.fileUpload This event fires immediately when you upload a file with wrong format or wrong size.
fileAdd.mdb.fileUpload This event fires immediately when a file has been uploaded in to file upload component.
fileRemove.mdb.fileUpload This event fires immediately when a file has been remove from file upload component.

For example, to listen on file add event and get that file:


        const fileUpload = document.getElementById('file-upload');
        fileUpload.addEventListener('fileAdd.mdb.fileUpload', function(e) { 
          const addedFile = e.files; 
        });
      

Import

MDB UI KIT also works with module bundlers. Use the following code to import this component:


        import { FileUpload } from 'mdb-ui-kit';