Class PasteFileHandler
java.lang.Object
com.vaadin.flow.component.clipboard.PasteFileHandler
- All Implemented Interfaces:
Serializable
Factory for paste-aware
UploadHandlers that group the parallel fetch
uploads of a single paste gesture and surface the result to application code.
Two flavours are offered, both producing an UploadHandler suitable
for
Clipboard.onFilePaste:
perFile(SerializableConsumer)— a single per-file callback that receives every file as aPasteFile, withPasteFile.newPaste()flagging the first file of each paste.batch()— a three-step batch listener:onStartfires once per paste with the declared file count,onFilefires per file, andonCompletefires once after the last file has been delivered.
onStart /
onFile / onComplete lifecycle, and no file is dropped on the
application's behalf. Batch state is tracked per owning component, so it is
released when the component is detached or the session ends rather than
living in the handler — a handler instance shared across components or
UIs cannot accumulate state. As a further bound, only a limited number of
unfinished pastes are tracked per component at once: if a paste's declared
Clipboard.PASTE_FILE_COUNT_HEADER count never fully arrives (a
stalled upload, or a missing/garbage header), the oldest such batch is
dropped once that many are open. The limit is far above any real overlap, so
it only ever trims abandoned or malformed batches.
Application code that wants "show the latest paste only" semantics tracks the highest paste id seen and filters in its own callbacks — the handler does not drop any files of a live paste on the application's behalf.
Resource notes
perFile(SerializableConsumer)and theonFilestep ofbatch()read each uploaded file fully into memory as abyte[]before invoking the callback. A very large paste is therefore held entirely in heap; cap it with the request/file size limits on a customUploadHandler, or read the stream yourself, when that matters.PasteFile.fileName()is the browser-supplied name and is not sanitized. Treat it as untrusted — never use it directly as a filesystem path without sanitizing it first.
- Since:
- 25.2
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classFluent builder for the batch paste handler. -
Method Summary
Modifier and TypeMethodDescriptionbatch()Starts a batch handler builder that emitsonStartonce per paste before any file,onFileper file, andonCompleteonce the paste's declared file count has been delivered.static UploadHandlerperFile(SerializableConsumer<PasteFile> consumer) Builds anUploadHandlerthat reads each upload fully into memory, packages it as aPasteFile, and delivers it to the supplied consumer on the UI thread.
-
Method Details
-
perFile
Builds anUploadHandlerthat reads each upload fully into memory, packages it as aPasteFile, and delivers it to the supplied consumer on the UI thread.- Parameters:
consumer- invoked for each accepted file on the UI thread, notnull- Returns:
- an upload handler suitable for
Clipboard.onFilePaste
-
batch
Starts a batch handler builder that emitsonStartonce per paste before any file,onFileper file, andonCompleteonce the paste's declared file count has been delivered. Any callback may be omitted.- Returns:
- a fresh, unconfigured batch builder
-