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 TypeMethodDescriptionvoidattachment(String fileName) Sets the Content-Disposition header to attachment with a filename, so that the browser saves the content as a file instead of showing it.voidSets the name of the file, which the browser uses when the content is saved.Returns 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) Deprecated, for removal: This API element is subject to removal in a future version.
-
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
Deprecated, for removal: This API element is subject to removal in a future version.useattachment(String), which behaves the same for a non-null name; unlike this method,attachment(null)still sends a bareattachmentheader. Or usefileName(String)to let the place where the handler is used decide whether the content is saved or shownSets the name of the file to be downloaded and serves it as an attachment. 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. Control characters, such as line breaks, are removed from the file name.If the Content-Disposition header was set directly on the response before any of these methods wrote it, this method will not override it.
The header is always
attachment, even when the place where the handler is used asks for the content to be shown inline, for exampleDownload.onClickwith a new tab.- Parameters:
fileName- the name to be assigned to the file
-
fileName
Sets the name of the file, which the browser uses when the content is saved. This does not change whether the content is saved or shown: that is decided byinline()orattachment(String)if the handler calls them, before or after this method, and otherwise by the place where the handler is used, through theDownloadDispositionof the resource the handler is registered with. For example,Download.onClickwith a new tab shows the content. Without any of these, the content is saved as an attachment.This method utilizes the HTTP Content-Disposition header. To be called before the response is committed.
If the
fileNameisnullor blank, this method does nothing. Control characters, such as line breaks, are removed from the file name.If the Content-Disposition header was set directly on the response before any of these methods wrote it, this method will not override it.
- Parameters:
fileName- the name of the file- Since:
- 25.4
- See Also:
-
attachment
Sets the Content-Disposition header to attachment with a filename, so that the browser saves the content as a file instead of showing it.The header is
attachmenteven when the place where the handler is used asks for the content to be shown inline. UsefileName(String)to let that place decide.To be called before the response is committed.
If the
fileNameisnullor blank, the file name given earlier, if any, is kept. Control characters, such as line breaks, are removed from the file name.If the Content-Disposition header was set directly on the response before any of these methods wrote it, this method will not override it.
- Parameters:
fileName- the name of the saved file- Since:
- 25.4
-
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. A file name given withfileName(String)is kept.The header is
inlineeven when the place where the handler is used asks for the content to be saved as a file. UsefileName(String)alone to let that place decide.To be called before the response is committed.
If the Content-Disposition header was set directly on the response before any of these methods wrote it, this method will not override it.
- Since:
- 25.0
-
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.The header is
inlineeven when the place where the handler is used asks for the content to be saved as a file. UsefileName(String)alone to let that place decide.To be called before the response is committed.
If the
fileNameisnullor blank, this behaves the same as callinginline(). Control characters, such as line breaks, are removed from the file name.If the Content-Disposition header was set directly on the response before any of these methods wrote it, this method will not override it.
- Parameters:
fileName- the suggested name for the file if saved by the user- Since:
- 25.0
-
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
-
attachment(String), which behaves the same for a non-null name; unlike this method,attachment(null)still sends a bareattachmentheader.