Class DownloadEvent
It gives direct access to the underlying request, response and session as well as various helpers specifically for handling downloads.
- Since:
- 24.8
-
Constructor Summary
ConstructorsConstructorDescriptionDownloadEvent(VaadinRequest request, VaadinResponse response, VaadinSession session, Element owningElement) -
Method Summary
Modifier and TypeMethodDescriptionReturns aOutputStreamfor writing binary data in the response.Get ownerComponentfor this event.Get the owning element for the download related to this event.GetVaadinRequestfor download event.GetVaadinResponsefor download event.GetVaadinSessionfor download event.getUI()Get the current UI instance for this request that can be used to make asynchronous UI updates withUI.access(com.vaadin.flow.server.Command).Returns aPrintWriterobject that can send character text to the client.voidinline()Sets the Content-Disposition header to inline, allowing the content to be displayed directly in the browser instead of being downloaded.voidSets the Content-Disposition header to inline with a filename, allowing the content to be displayed directly in the browser with a suggested filename if the user chooses to save it.voidsetContentLength(long contentLength) Sets the length of the content body in the response if the length is not-1.voidsetContentType(String contentType) Sets the content type for the current download.voidsetFileName(String fileName) Sets the name of the file to be downloaded.
-
Constructor Details
-
DownloadEvent
public DownloadEvent(VaadinRequest request, VaadinResponse response, VaadinSession session, Element owningElement)
-
-
Method Details
-
getOutputStream
Returns aOutputStreamfor writing binary data in the response.Either this method or getWriter() may be called to write the response, not both.
- Returns:
- a
OutputStreamfor writing binary data - Throws:
UncheckedIOException- if an I/O error occurred while getting the output stream
-
getWriter
Returns aPrintWriterobject that can send character text to the client. The PrintWriter uses the character encoding defined using setContentType.Either this method or getOutputStream() may be called to write the response, not both.
- Returns:
- a
PrintWriterfor writing character text - Throws:
UncheckedIOException- if an I/O error occurred while getting the writer
-
getRequest
GetVaadinRequestfor download event.- Returns:
- vaadin request
-
getResponse
GetVaadinResponsefor download event.- Returns:
- vaadin response
-
getSession
GetVaadinSessionfor download event.- Returns:
- vaadin session
-
setFileName
Sets the name of the file to be downloaded. This method utilizes the HTTP Content-Disposition header to specify the name of the file to be downloaded.To be called before the response is committed.
If the
fileNameisnull, the Content-Disposition header won't be set.If the Content-Disposition header has already been set, this method will not override it.
- Parameters:
fileName- the name to be assigned to the file
-
inline
public void inline()Sets the Content-Disposition header to inline, allowing the content to be displayed directly in the browser instead of being downloaded.To be called before the response is committed.
If the Content-Disposition header has already been set, this method will not override it.
-
inline
Sets the Content-Disposition header to inline with a filename, allowing the content to be displayed directly in the browser with a suggested filename if the user chooses to save it.To be called before the response is committed.
If the
fileNameisnullor blank, this behaves the same as callinginline().If the Content-Disposition header has already been set, this method will not override it.
- Parameters:
fileName- the suggested name for the file if saved by the user
-
setContentType
Sets the content type for the current download. These methods utilize the HTTP Content-Type header to specify the type of content being sent to the client.To be called before the response is committed.
- Parameters:
contentType- the MIME type to set as the content type
-
setContentLength
public void setContentLength(long contentLength) Sets the length of the content body in the response if the length is not-1. This method utilizes the HTTP Content-Length header to specify the length of the content being sent to the client.To be called before the response is committed.
- Parameters:
contentLength- the length of the response content in bytes
-
getOwningComponent
Get ownerComponentfor this event.The download handler may change the component's state during download, e.g. disable or hide it during download or get the component's own data like id.
- Returns:
- owning component or null in none defined
-
getOwningElement
Get the owning element for the download related to this event.The download handler may use element's attributes or properties to define what to download or change the element, e.g. element's id or data id to fetch a row from a database or disable element once the download is started.
- Returns:
- owning element
-
getUI
Get the current UI instance for this request that can be used to make asynchronous UI updates withUI.access(com.vaadin.flow.server.Command).- Returns:
- Current UI instance
-