Docs

Documentation versions (currently viewingVaadin 14)

You are viewing documentation for an older Vaadin version. View latest documentation

Upload

The Upload component allows the user to upload files to the server-side. The component supports single or multiple-file uploads and drag and drop.

Using Upload

Defining a Receiver Instance

A Receiver instance should be specified to receive uploaded data. The receiver provides an OutputStream for the framework, where it writes the content of the received file. You can implement the interface directly, or use the prebuilt helpers that buffer the file content for later access.

There are a number of built-in implementations of Receiver:

  • FileBuffer

  • MemoryBuffer

  • MultiFileBuffer

  • MultiFileMemoryBuffer.

The last two implement MultiFileReceiver and can be used to upload multiple files at once.

Example: Simple Receiver instance that allows uploading of one file.

MemoryBuffer memoryBuffer = new MemoryBuffer();

Upload upload = new Upload(memoryBuffer);
upload.addFinishedListener(e -> {
    InputStream inputStream = memoryBuffer.getInputStream();
    // read the contents of the buffered memory
    // from inputStream
});
  • The uploaded data is stored in a file on the file system.

  • You can read the data using the getInputStream() method in MemoryBuffer when the upload is finished.

You can use a MultiFileReceiver to allow uploading of more than one file.

Example: Using MultiFileMemoryBuffer

MultiFileMemoryBuffer multiFileMemoryBuffer = new MultiFileMemoryBuffer();
Upload upload = new Upload(multiFileMemoryBuffer);
  • The MultiFileMemoryBuffer class is a ready-made implementation of MultiFileReceiver.

  • The data is stored in files on the file system and you can get the data using a file name and the getInputStream(String) method.

Setting Maximum Files

You can limit the number of files that can be uploaded at a time using the setMaxFiles method.

C52AA3CB-6A2E-4CB1-9F4E-08B583D45136