Vaadin Elements - Upload - Basic Usage

Basic Usage

The user can select files to upload by using the file selector in the browser or by dropping the files from the desktop into the component.

<vaadin-upload target=""></vaadin-upload>


You can restrict the type and size of files, and the maximum number of files to upload, by using either element attributes or the JavaScript API.

The validation is done just before the request is sent to the server. It fails if any of the conditions don’t match, in which case a notice is shown to the user.

While dragging is being done, we cannot read the types, names, and sizes of the files due to security reasons. Only when the file is dropped into the component, we can access the dataTransfer.files, and here is when we do the validation and show the notice.

The properties that the user can configure for validation are:


Meaning the max number of files that the user can select to upload. When the list is full, the user may add new files after removing some files from the list.


The maximum allowed file size in bytes.


A filter for the types of files the user can upload. A filter is a comma-separated list of allowed MIME types or file extensions. Browsers use this information to filter out non-conforming files when prompting the user to select files. Notice that although the HTML-4.01 file upload specification using MIME types patterns is widely supported, only certain browsers allow the file extension syntax introduced in the HTML5 specification.

Nevertheless, this component will always validate file types and extensions before sending any file to the server, despite differences in browser syntax support.

You can use validation parameters as follows:

<vaadin-upload max-files="3" max-file-size="1000000"


The vaadin-upload element dispatches events when files are added, removed, or rejected, and for every upload state change for each file. You can use the events to change the behaviour of the upload process. Such customizations may require preventing the default actions, which you can do with preventDefault().

Event Details

All the events contain the File object in the event.detail.file property. The object has a number of extra properties to track the upload process:


The target URL used to upload this file.


Elapsed time since the upload started.


Human-readable elapsed time.


Number of seconds remaining for the upload to finish.


Human-readable remaining time for the upload to finish.


Upload speed in kB/s.


File size in bytes.


Human-readable total size of the file.


Bytes transferred so far.


Human-readable uploaded size at the moment.


Status of the upload process.


Error message in case the upload failed.


Percentage of the file already uploaded.


True when the file was transferred to the server.


True while transferring data to the server.

Additionally, upload events also have an event.detail.xhr property with the corresponding XMLHttpRequest instance.

In the following example, we listen to upload-start events, which are fired when the file upload process starts.

var upload = document.querySelector('vaadin-upload');
upload.addEventListener('upload-start', function(e) {
  alert("Uploading " +;

When using the element inside another Polymer element, you can use the declarative binding syntax to subscribe for an event:

<dom-module id="my-element">
    <vaadin-upload on-upload-start="uploadStarted"></vaadin-upload>
    is: 'my-element',
    uploadStarted: function(event) {
      alert("Uploading " +;

Event List

The following events are fired by the component in different phases of the upload process:


Fired when a file cannot be added to the queue due to a validation constraint.


Fired when file abort is requested. If the default is prevented, the file upload will not be aborted.


Fired before the XHR is opened. It is useful for changing the request URL based on the file name, etc.


Fired if the upload process failed.


Fired as many times as a file progress is updated.


Fired when the request has been opened but not yet sent. It is useful for appending additional data keys to the request and for changing some parameters such as headers. If the event is default-prevented, then the request is not sent to the server. In this case, you can send the XHR manually.


Fired when the server response was received, but before the component processes it. It is useful for making the upload fail depending on the response. If the event is default-prevented, the vaadin-upload skips the default flow, allowing the developer to do something on his own, such as retrying the upload.


Fired when the upload is retried. If the default is prevented, retry would not be performed.


Fired when the XHR is sent.


Fired if the upload process succeeds.