Skip to main content

Upload

The Upload component allows users to select and upload files via browsing or drag-and-drop. It supports configurable file type and size validation, multiple file uploads, and provides visual feedback for each file's upload status.

Import

// with @dhl-official/react-library:
import { DhlUpload } from "@dhl-official/react-library"
// with @dhl-official/ui-libraries/react-library:
import { DhlUpload } from "@dhl-official/ui-libraries/react-library"
info

DhlUpload is a presentational component only — it does not send files to a server. It handles file selection, drag-and-drop, and client-side validation (size/type), but performing the actual upload request and reporting its outcome back to the component is the consumer's responsibility. See Handling File Uploads below.

File Statuses

Each entry in files has a status field (type DhlUploadFileItemStatus) that drives what dhl-upload-file-item renders. dhl-upload only ever sets the first two of these — moving a file to success/done or error after that point is done by the consumer, based on the outcome of the real upload request.

StatusSet byRendered asWhen to use it
in-progressdhl-upload (built-in)Spinner + "Uploading..."Automatically set right after a file passes client-side validation.
errordhl-upload (built-in) or consumerError icon + statusMessageAutomatically set when validation fails (size/type). Also set this yourself if your upload request fails.
successConsumerSuccess ring + statusMessageSet this once your upload request resolves, to show a brief confirmation state.
doneConsumerStatic file icon + fileSizeThe resting state for a completed upload, e.g. after a success confirmation or once the user clicks "Continue".

Handling File Uploads

Since dhl-upload doesn't perform the network request itself, wire it up as follows:

  1. Listen for the dhlUploadFilesAdded event. It fires with { files } right after client-side validation, and each new entry already has status: "in-progress".
  2. In your handler, send the file(s) to your own backend (e.g. via fetch or XMLHttpRequest).
  3. Once the request settles, update that file's entry back into the files prop with status: "done" on success, or status: "error" (with a statusMessage) on failure. files is designed to be a controlled prop — pass your updated array back in.

Skipping step 3 is the most common integration mistake — it's what leaves the spinner from step 1 stuck permanently, since nothing else in the component will ever clear it.

function InvoiceUpload() {
const [files, setFiles] = useState([]);

const updateFile = (id, patch) =>
setFiles((prev) => prev.map((f) => (f.id === id ? { ...f, ...patch } : f)));

const handleFilesAdded = async ({ files: added }) => {
setFiles(added); // shows the in-progress spinner immediately

for (const entry of added) {
if (entry.status !== "in-progress") continue; // skip files that failed validation

try {
const form = new FormData();
form.append("file", entry.file);

const res = await fetch("/api/customs-invoices", { method: "POST", body: form });
if (!res.ok) throw new Error("Upload failed");

updateFile(entry.id, { status: "done" });
} catch {
updateFile(entry.id, {
status: "error",
statusMessage: "Upload failed, please try again",
});
}
}
};

return (
<DhlUpload
acceptedTypes=".pdf,.docx,.odt,.rtf"
maxSize={31457280}
multiple
files={files}
onDhlUploadFilesAdded={handleFilesAdded}
/>
);
}
caution

Always handle the failure path. If only the success branch sets a new status, a network error or timeout leaves the file stuck on in-progress indefinitely — the same symptom as never wiring up the upload at all.

Code

<DhlUpload
acceptedTypes=".pdf,.docx,.odt,.rtf"
maxSize={31457280}
multiple
/>

Interactive Demo

Readme

Properties

PropertyAttributeDescriptionTypeDefault
acceptedTypesaccepted-typesAn optional prop defining a comma-separated list of accepted file types. Supports MIME types and extensions (e.g. ".pdf,.docx" or "application/pdf,image/*").stringDHL_UPLOAD.DEFAULTS.ACCEPTED_TYPES
backgroundbackgroundAn optional prop to set the background of the component container. Defaults to transparent."transparent" | "white"DHL_UPLOAD.BACKGROUND.TRANSPARENT
browseButtonLabelbrowse-button-labelAn optional prop for the browse button label.string"Browse Files"
cancelButtonLabelcancel-button-labelAn optional prop for the cancel button label.string"Cancel"
continueButtonLabelcontinue-button-labelAn optional prop for the continue button label.string"Continue"
dataAriaLabeldata-aria-labelAn optional prop defining the text read by the screen reader.string"File upload"
dataClassNamedata-class-nameAn optional class name prop for the component.stringundefined
dataIddata-idAn optional prop. Gives a valid HTML ID attribute value for the component.string`dhl-upload-${getRandomString()}`
dataMaskPiidata-mask-piiAn optional prop to mask sensitive data in session-replay tools. When true, sets data-di-mask on the inner element.booleanundefined
dataTestiddata-testidAn optional prop. The test id attached to the component as a data-testid attribute.stringundefined
descriptiondescriptionAn optional prop for the description text displayed in the drop zone.string"Or drop here to upload"
filesfilesAn optional prop for the list of files rendered in the upload. The component manages this list in response to user interaction, but consumers may provide an initial value or take control by updating it externally. Each entry follows the DhlUploadFile interface.DhlUploadFile[][]
isDisabledis-disabledAn optional flag to define if the component is disabled.booleanfalse
maxSizemax-sizeAn optional prop for the maximum file size in bytes. Defaults to 30 MB.numberDHL_UPLOAD.DEFAULTS.MAX_SIZE
maxSizeLabelmax-size-labelAn optional prop for the max size display text. If not set, the value is auto-generated from the maxSize prop.stringundefined
multiplemultipleAn optional flag to allow multiple file uploads.booleanDHL_UPLOAD.DEFAULTS.MULTIPLE
showBordershow-borderAn optional flag to display the dashed border around the drop zone.booleanDHL_UPLOAD.DEFAULTS.SHOW_BORDER
showCtashow-ctaAn optional flag to display the Cancel/Continue action buttons.booleanDHL_UPLOAD.DEFAULTS.SHOW_CTA
showDescriptionshow-descriptionAn optional flag to display the description text.booleanDHL_UPLOAD.DEFAULTS.SHOW_DESCRIPTION
showFileTypesshow-file-typesAn optional flag to display the accepted file types.booleanDHL_UPLOAD.DEFAULTS.SHOW_FILE_TYPES
showFilesshow-filesAn optional flag to display the file list.booleanDHL_UPLOAD.DEFAULTS.SHOW_FILES
showIconshow-iconAn optional flag to display the upload icon in the drop zone.booleanDHL_UPLOAD.DEFAULTS.SHOW_ICON

Events

EventDescriptionType
dhlUploadCancelEvent emitted when the cancel button is clicked.CustomEvent<void>
dhlUploadContinueEvent emitted when the continue button is clicked.CustomEvent<{ files: DhlUploadFile[]; }>
dhlUploadFileRemovedEvent emitted when a file is removed from the list.CustomEvent<{ fileId: string; fileName: string; }>
dhlUploadFilesAddedEvent emitted when files are added to the upload (after validation).CustomEvent<{ files: DhlUploadFile[]; }>

Dependencies

Depends on

Graph


Built by DHL User Interface Library Team!