Documentation

Documentation versions (currently viewingVaadin 23)

You are viewing documentation for Vaadin 23. View latest documentation

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.

The built-in implementations of Receiver are:

  • 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.

EB618652-4822-49DC-9A51-D71237EF100E