Skip to main content

File Upload Hds

<sl-file-upload-hds> | SlFileUploadHds
Since 2.0 experimental

A file uploader allows users to upload and attach one or more files. It shows the upload progress and status of each file.

<sl-file-upload-hds full-screen-drag></sl-file-upload-hds>

Examples

Getting the deleted file

<sl-file-upload-hds id="file-upload-deleted"></sl-file-upload-hds>

<script type="module">
  const fileUploadDeleted = document.querySelector("#file-upload-deleted");

  fileUploadDeleted.addEventListener('sl-remove', () => {
      console.log('fileUploadDeleted', fileUploadDeleted.deletedFileForCapture)
  });
</script>

Access the selected fileset

<sl-file-upload-hds id="file-upload-fileset"></sl-file-upload-hds>

<script type="module">
  const fileUploadFileSet = document.querySelector("#file-upload-fileset");

  fileUploadFileSet.addEventListener('sl-input', () => {
      console.log('fileUploadFileSet', fileUploadFileSet.selectedFilesSet)
  });
</script>

Push an error for a file

The below is an example of how the backend could push an error for an individual file

<sl-file-upload-hds id="file-upload-be-error"></sl-file-upload-hds>

<script type="module">
  const fileUploadError = document.querySelector("#file-upload-be-error");
  const errorMessage = 'The backend rejected this file because of its content.';

  fileUploadError.addEventListener('sl-input', () => {
      fileUploadError.setFileError(fileUploadError.fileDetails.name, errorMessage);
  });
</script>

Getting file content

The component will emit a sl-input event when a file of an appropriate type has been dropped.

<sl-file-upload-hds id="file-upload-1"></sl-file-upload-hds>

<script type="module">
  const fileUpload1 = document.querySelector("#file-upload-1");

  fileUpload1.addEventListener('sl-input', () => {
      console.log("File content: ", fileUpload1.value);
      console.log('fileUpload1', fileUpload1.selectedFilesSet)
  });
</script>

Small File Upload

<sl-file-upload-hds id="file-upload-22" upload-component-size="small"></sl-file-upload-hds>

Large File Upload

<sl-file-upload-hds id="file-upload-22" upload-component-size="large">
  <slot name="large">
    <div>Read more on how to <a href="#c">get the most out of bulk uploading.</a></div>
  </slot>
</sl-file-upload-hds>

Disabled

  <sl-file-upload-hds disabled></sl-file-upload-hds>

Output data types

Use the output-type attribute to use different types of output.

Allowed types are ‘data-url’ | ‘text’ | ‘array-buffer’

Drop a text file here and check the console log
<sl-file-upload-hds id="file-upload-2" output-type="text" allowed-types='["txt"]' unsupported-type-message="Only text files are allowed here!">
  Drop a text file here and check the console log
</sl-file-upload-hds>

<script type="module">
  const fileUpload2 = document.querySelector("#file-upload-2");

  fileUpload2.addEventListener('sl-input', () => {
      console.log("File content: ", fileUpload2.value);
  });
</script>

Getting file meta data

The fileDetails property returns an object with the file name, size, type, and last modified date.

<sl-file-upload-hds id="file-upload-3"></sl-file-upload-hds>

<script type="module">
  const fileUpload3 = document.querySelector("#file-upload-3");

  fileUpload3.addEventListener('sl-input', () => {
      console.log("File meta data: ", fileUpload3.fileDetails);
  });
</script>

Restricting allowed file types

Use the allowed-types attribute to control the allowed types. Note the single/double quote pattern. This pattern is required for the deserialisation of the array.

Use the unsupported-type-message attribute to set a custom message when an unsupported file type is uploaded.

Only SVG and PNG files are allowed here

<sl-file-upload-hds
    id="branding-logo-file-upload"
    allowed-types='["svg", "png"]'
    unsupported-type-message="Only SVG and PNG files are supported here!!!!"
    output-type="data-url"

    >
  <p>Only SVG and PNG files are allowed here</p>
</sl-file-upload-hds>

<script>
  (() => {
    const fileUpload = document.querySelector('#branding-logo-file-upload');

    fileUpload.addEventListener('sl-input', () => {
      console.log("file uploaded", fileUpload.value);
      document.querySelector('#branding-logo-upload-complete-alert').toast();
    });
  })();
</script>

[component-metadata:sl-file-upload-hds]

Slots

Name Description
(default) The default slot.
small A slot for the small component.
medium A slot for the medium component.
large A slot for the large component.
help A slot for the help text.

Learn more about using slots.

Properties

Name Description Reflects Type Default
allowedTypes
allowed-types
Allowed file types. If empty, then all supported types are allowed. string[] []
outputType
output-type
Output type. Default is data-url 'data-url' | 'text' | 'array-buffer' 'data-url'
unsupportedTypeMessage
unsupported-type-message
The message to show when a file type that is not supported is dropped string ''
loadingLabel
loading-label
The label to show when loading a file string 'Loading Label'
maxFileSize
max-file-size
The maximum file size in MB number 5
maxFiles
max-files
The maximum number of files that can be uploaded number 10
disabled Disables the file upload boolean false
alertMessage
alert-message
Custom alert message string 'Default error message'
uploadComponentSize
upload-component-size
Display large, medium or small version of the component 'small' | 'medium' | 'large' 'medium'
fullScreenDrop
full-screen-drop
Toggle full screen drop zone boolean false
contentElement The container for the drop zone HTMLElement -
errorAlert Error container SlAlert -
fileInput The file input element HTMLInputElement -
updateComplete A read-only promise that resolves when the component has finished updating.

Learn more about attributes and properties.

Events

Name React Event Description Event Detail
sl-input Emitted when a file of valid type is dropped. -

Learn more about events.

Custom Properties

Name Description Default
--example An example CSS custom property.

Learn more about customizing CSS custom properties.

Parts

Name Description
base The component’s base wrapper.

Learn more about customizing CSS parts.

Dependencies

This component automatically imports the following dependencies.

  • <sl-example>