You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -14,118 +14,210 @@ The uploading of files may be handled either with Vaadin Flow using Java, or wit
14
14
15
15
== Handling Uploaded Files in Flow
16
16
17
-
The Java Flow Upload component provides an API to handle directly uploaded file data, without having to set up an endpoint or a servlet. It uses a [classname]`Receiver` implementation to write the incoming file data into an [classname]`OutputStream`.
17
+
The Java Flow Upload component provides an API to handle directly uploaded file data, without having to set up an endpoint or a servlet. It uses a [classname]`UploadHandler` implementation to handle the incoming file data.
18
18
19
-
The following built-in implementations of [classname]`Receiver` are available:
19
+
The [classname]`UploadHandler` is a functional interface that only requires the [methodame]`handleUploadRequest(UploadEvent)` to be implemented for receiving data or use one of the built-in implementations.
20
20
21
-
- [classname]`MemoryBuffer`;
22
-
- [classname]`MultiFileMemoryBuffer`;
23
-
- [classname]`FileBuffer`; and
24
-
- [classname]`MultiFileBuffer`.
21
+
The following built-in implementations of [classname]`UploadHandler` are available:
25
22
26
-
These are described in the sub-sections that follow.
23
+
- [classname]`InMemoryUploadHandler`, Stores uploaded files in memory
24
+
- [classname]`FileUploadHandler`, Saves uploaded files to the file system
25
+
- [classname]`TemporaryFileUploadHandler`, Saves uploaded files to temporary files
27
26
27
+
These are described in the sub-sections that follow.
28
+
All of the build-in implementations extend [classname]`TransferProgressAwareHandler` and support <<add-progress-listener, adding progress listeners>>.
28
29
29
-
=== MemoryBuffer
30
+
=== InMemoryUploadHandler
30
31
31
-
The [classname]`MemoryBuffer` can handle a single file upload at once. It does so by writing the file data into an in-memory buffer.
32
+
The [classname]`InMemoryUploadHandler` stores uploaded files in memory as byte arrays.
32
33
33
-
Using [classname]`MemoryBuffer` will configure the component so that only a single file can be selected.
34
+
The handler is given a `successhandler` of type `SerializableBiConsumer<UploadMetadata, byte[]> successHandler` that is called for each successful upload.
35
+
The `successhandler` callback contains [classname]`UploadMetadata` with information on the uploaded file and a `byte[]` that contains the actual data from the upload.
The [classname]`FileUploadHandler` saves the upload as a file to the file system.
53
56
54
-
=== MultiFileMemoryBuffer
57
+
The handler is given a `successhandler` of type `SerializableBiConsumer<UploadMetadata, File> successHandler` that is called for each successful upload and a [classname]`FileFactory`.
58
+
The `successhandler` callback contains [classname]`UploadMetadata` with information on the uploaded file and the actual [classname]`File` where the data was written.
55
59
56
-
The [classname]`MultiFileMemoryBuffer` is the same as [classname]`MemoryBuffer`, but it can handle multiple file uploads at once. It writes the file data into a set of in-memory buffers.
60
+
The file used is defined with [classname]`FileFactory` that gets the filename for the upload and should return the [classname]`File` to store the data into.
57
61
58
62
[source,java]
59
63
----
60
-
MultiFileMemoryBuffer multiFileMemoryBuffer = new MultiFileMemoryBuffer();
61
-
62
-
Upload upload = new Upload(multiFileMemoryBuffer);
The [classname]`TemporaryFileUploadHandler` works the same as [classname]`FileUploadHandler`, except that instead of taking in a [classname]`FileFactory`, it stores the data into a temporary file in the system default temporary-file directory.
81
75
82
-
The [classname]`FileBuffer` can handle a single file upload at once. It saves the file on the file system, in the current working directory of the Java application.
83
-
84
-
Using [classname]`FileBuffer` will configure the component so that only a single file can be selected.
76
+
The handler is given a `successhandler` of type `SerializableBiConsumer<UploadMetadata, File> successHandler` that is called for each successful upload and a [classname]`FileFactory`.
77
+
The `successhandler` callback contains [classname]`UploadMetadata` with information on the uploaded file and the actual [classname]`File` where the data was written.
System.out.printf("File saved to: %s%n", absolutePath);
98
-
});
86
+
Upload upload = new Upload(temporaryFileHandler);
99
87
----
100
88
89
+
=== Custom UploadHandler Implementations
101
90
102
-
=== MultiFileBuffer
91
+
For more advanced use cases, you can provide custom implementations for [classname]`UploadHandler`.
92
+
You might do this, for example, to save files into a specific directory, or to upload them to cloud storage.
103
93
104
-
The [classname]`MultiFileBuffer` works the same as [classname]`FileBuffer`, except that it can handle multiple file uploads at once. For each file, it saves the file on the file system, in the current working directory of the Java application.
94
+
[classname]`UploadHandler` is a [annotationname]`FuncionalInterface` so it can be implemented or just be a lambda expression.
105
95
106
96
[source,java]
107
97
----
108
-
MultiFileBuffer fileBuffer = new MultiFileBuffer();
To add progress tracking to a custom upload handler, you can extend [classname]`TransferProgressAwareHandler`:
126
132
127
-
For more advanced use cases, you can provide custom implementations for [classname]`Receiver` or [classname]`MultiFileReceiver`. You might do this, for example, to save files into a specific directory, or to upload them to cloud storage.
page-title: How to use UploadHandler in Vaadin Flow
4
+
description: Using UploadHandler to receive an incoming data stream.
5
+
meta-description: Learn to receive content using ElementRequestHandler in Vaadin Flow.
6
+
order: 130
7
+
---
8
+
9
+
10
+
= Using UploadHandler to Receive an Incoming Data Stream
11
+
12
+
To receive an upload from the client, you need to register an [classname]`UploadHandler` that accepts a URL to handle receiving an upload stream.
13
+
14
+
To create an [classname]`UploadHandler`, you need implement the [methodname]`handleUploadRequest` to handle the upload or use one of the pregenerated UploadHandlers.
15
+
The existing [classname]`UploadHandler` implementations can be gotten through the static methods in [classname]`UploadHandler`, e.g. `UploadHandler.toTempFile(SerializableBiConsumer<UploadMetadata, File> successHandler)`
16
+
17
+
[NOTE]
18
+
When receiving a multipart upload the [methodname]`handleUploadRequest` will be called separately for each file in the upload.
19
+
20
+
Then the handler can be registered through the [classname]`Element` API after registering it to the [classname]`StreamResourceRegistration`.
0 commit comments