Skip to content

MazDropzone

MazDropzone is a powerful and flexible file upload component with drag & drop support, progress tracking, and customizable UI. Perfect for handling single or multiple file uploads in your Vue applications.

Before use it, you should install the MazUi plugin in your project, follow instructions in Getting Started

TIP

This component can be translated globally using the MazUi plugin.

Key Features

  • 🎯 Drag & drop support
  • 📁 Single or multiple file uploads
  • 🖼️ Image preview thumbnails
  • 🚀 Auto upload support
  • ⚡ Async file processing
  • 🎨 Fully customizable UI
  • 🌐 Support for various file types
  • ⚙️ Extensive configuration options
  • 🔒 File validation (size, type, count)
  • 🌍 Internationalization support

Error codes

Error CodeDescriptionTrigger
FILE_SIZE_EXCEEDEDFile size exceeds the allowed limitWhen a file is larger than maxFileSize (in MB)
FILE_SIZE_TOO_SMALLFile size is below the minimum limitWhen a file is smaller than minFileSize (in MB)
MAX_FILES_EXCEEDEDMaximum number of allowed files exceededWhen trying to add more files than maxFiles or more than 1 file if multiple=false
FILE_TYPE_NOT_ALLOWEDFile type is not allowedWhen the file's MIME type doesn't match the specified dataTypes
FILE_DUPLICATEDAn identical file already existsWhen allowDuplicates=false and a file with the same name, size and type already exists
NO_FILES_TO_UPLOADNo files to uploadWhen starting upload but there are no files in the dropzone
FILE_UPLOAD_ERRORError during individual file uploadWhen uploading a specific file fails (single mode)
FILE_UPLOAD_ERROR_MULTIPLEError during batch uploadWhen uploading multiple files fails (multiple mode)
NO_URLNo upload URL configuredWhen attempting auto-upload without defining the url prop

Error handling example

View code
vue
<script setup>
function handleError({ code, files }) {
  switch (code) {
    case 'FILE_SIZE_EXCEEDED':
      console.log('File too large:', files[0].name)
      break
    case 'FILE_TYPE_NOT_ALLOWED':
      console.log('File type not allowed:', files[0].type)
      break
    case 'MAX_FILES_EXCEEDED':
      console.log('Too many files selected')
      break
      // ...
  }
}
</script>

<template>
  <MazDropzone
    :max-file-size="5"
    :max-files="3"
    :data-types="['image/*', 'application/pdf']"
    :allow-duplicates="false"
    @error="handleError"
  />
</template>

Basic Usage


File Type Restrictions

You can restrict allowed file types using the data-types prop:

Auto Upload

Enable automatic file upload using the auto-upload prop. Files can be uploaded individually (single) in separate requests or all at once (multiple) in a single request:

You can also upload all files at once using multiple:

TIP

Multiple upload is allowed:

  • when max-files is greater than 1
  • when auto-upload is set to multiple
  • when multiple prop is set to true

Link to the source code

Custom Upload Area

Customize the upload area using slots:

Custom Translations

Using translations prop

Customize text messages using the translations prop:

Using MazUi plugin

You can also use the MazUi plugin to customize the translations:

View code
ts
import { MazUi } from 'maz-ui/plugins/maz-ui'

app.use(MazUi, {
  translations: {
    locale: 'en',
    messages: {
      en: {
        dropzone: {
          dragAndDrop: 'Drag files here',
          selectFile: 'select files',
          divider: 'or',
          fileMaxCount: 'Maximum {count} files',
          fileMaxSize: 'Maximum {size} MB',
          fileTypes: 'Allowed file types: {types}',
        },
      },
    },
  },
})

Custom Upload Request

Customize the upload request using uploadUrl, requestOptions and transformBody:

Props

NameDescriptionTypeRequiredDefault
idThe id of the dropzonestringNoundefined
multipleAllow multiple files to be uploaded.booleanNofalse
data-typesAllowed data types/MIME types for files (e.g. ['application/json'])string[]No['*\/*']
prevent-default-for-unhandledPrevent default behavior for unhandled drag & drop events.booleanNotrue
max-file-sizeMaximum file size in MB.numberNoundefined
max-filesMaximum number of files allowed.numberNoundefined
disabledDisable the dropzonebooleanNoundefined
previewShow file previewbooleanNoundefined
min-file-sizeMinimum file size in MBnumberNoundefined
allow-duplicatesAllow duplicatesbooleanNofalse
translationsTranslations

Custom translations for the component
Partial<MazUiTranslationsNestedSchema['dropzone']>Noundefined
colorMain color of the componentMazColorNoprimary
remove-file-btn-propsMazBtn props MazBtn propsMazBtnPropsNo{}
spinner-propsMazSpinner props MazSpinner propsMazSpinnerPropsNo{}
auto-uploadAuto upload files

If set to multiple, all files will be uploaded at once in a single request. If set to single, files will be uploaded one by one in separate requests. If set to false, no upload will be done automatically.
"multiple" | "single" | falseNofalse
urlUpload URL

If set, files will be uploaded to the given URL.
stringNoundefined
request-optionsRequest options

Request options to be used for the upload (using fetch) Request options

Example: { mode: 'no-cors', headers: { 'Content-Type': 'multipart/form-data', 'Authorization': 'Bearer 1234567890' } }
RequestInitNoundefined
transform-bodyTransform the body of the requestTSFunctionTypeNoundefined
max-concurrent-uploadsMaximum number of concurrent uploads

Limit the number of files uploaded simultaneously to avoid overwhelming the server
numberNo5

Events

Event namePropertiesDescription
dropvalues { files: File[] | null, event: DragEvent } - The dropped files and the drag eventEvent emitted when files are dropped in the dropzone
entervalues { files: File[] | null, event: DragEvent } - The dragged files and the drag eventEvent emitted when dragged files enter the dropzone
leavevalues { files: File[] | null, event: DragEvent } - The dragged files and the drag eventEvent emitted when dragged files leave the dropzone
overvalues { files: File[] | null, event: DragEvent } - The dragged files and the drag eventEvent emitted when dragged files are over the dropzone
addvalue File - The added fileEvent emitted when a file is added to the dropzone
removevalue File - The removed fileEvent emitted when a file is removed from the dropzone
errorvalues { files: File[] | null, event: DragEvent | null, code: MazDropzoneErrorCode } - The files, event and error codeEvent emitted when an error occurs
upload-errorvalues { file: File, code: MazDropzoneErrorCode, error: unknown } - The file, error code and error detailsEvent emitted when an error occurs during file upload
upload-error-multiplevalues { files: File[], code: MazDropzoneErrorCode, error: unknown } - The files, error code and error detailsEvent emitted when an error occurs during multiple files upload
upload-successvalues { file: File, response?: Response } - The uploaded file and the responseEvent emitted when a file is successfully uploaded
upload-success-multiplevalues { files: File[], response?: Response } - The uploaded files and the responseEvent emitted when multiple files are successfully uploaded

Slots

NameDescriptionBindings
files-areaSlot to customize the files areafiles-data MazDropzoneFileData[] - The files data
file-itemSlot to customize the file itemfile MazDropzoneFileData - The drop file data
no-files-areaSlot to customize the no files areaselect-file Function - The function to select the file
upload-iconSlot to customize the upload icon

Expose

uploadFiles

Upload files
@description With this method, the files are uploaded one by one (a request for each file)
@usage mazDropzoneInstance.value?.uploadFiles()

uploadFilesMultiple

Upload multiple files
@description With this method, the files are uploaded all at once in a single request
@usage mazDropzoneInstance.value?.uploadFilesMultiple()

getFormData

Get form data
@description Get the form data of one file
@usage const formData = mazDropzoneInstance.value?.getFormData(file)

getFormDataMultiple

Get form data multiple
@description Get the form data of all files
@usage const formData = mazDropzoneInstance.value?.getFormDataMultiple()

reset

Reset the files
@description Remove all files from the dropzone
@usage mazDropzoneInstance.value?.reset()

isUploading

Check if the files are uploading
@description Check if the files are uploading
@usage const isUploading = mazDropzoneInstance.value?.isUploading

addFile

Add a file to the dropzone
@description Add a file manually to the dropzone
@usage mazDropzoneInstance.value?.addFile(file)

removeFile

Remove a file from the dropzone
@description Remove a file manually from the dropzone
@usage mazDropzoneInstance.value?.removeFile(file)