Class DownloadEvent

java.lang.Object
com.vaadin.flow.server.streams.DownloadEvent

public class DownloadEvent extends Object
Class containing meta-data for handling the requested client download.

It gives direct access to the underlying request, response and session as well as various helpers specifically for handling downloads.

Since:
24.8
  • Constructor Details

  • Method Details

    • getOutputStream

      public OutputStream getOutputStream()
      Returns a OutputStream for writing binary data in the response.

      Either this method or getWriter() may be called to write the response, not both.

      Returns:
      a OutputStream for writing binary data
      Throws:
      UncheckedIOException - if an I/O error occurred while getting the output stream
    • getWriter

      public PrintWriter getWriter()
      Returns a PrintWriter object 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 PrintWriter for writing character text
      Throws:
      UncheckedIOException - if an I/O error occurred while getting the writer
    • getRequest

      public VaadinRequest getRequest()
      Get VaadinRequest for download event.
      Returns:
      vaadin request
    • getResponse

      public VaadinResponse getResponse()
      Get VaadinResponse for download event.
      Returns:
      vaadin response
    • getSession

      public VaadinSession getSession()
      Get VaadinSession for download event.
      Returns:
      vaadin session
    • setFileName

      @Deprecated(since="25.4", forRemoval=true) public void setFileName(String fileName)
      Deprecated, for removal: This API element is subject to removal in a future version.
      use attachment(String), which behaves the same for a non-null name; unlike this method, attachment(null) still sends a bare attachment header. Or use fileName(String) to let the place where the handler is used decide whether the content is saved or shown
      Sets 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 fileName is null, 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 example Download.onClick with a new tab.

      Parameters:
      fileName - the name to be assigned to the file
    • fileName

      public void fileName(String 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 by inline() or attachment(String) if the handler calls them, before or after this method, and otherwise by the place where the handler is used, through the DownloadDisposition of the resource the handler is registered with. For example, Download.onClick with 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 fileName is null or 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
      See Also:
    • attachment

      public void attachment(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.

      The header is attachment even when the place where the handler is used asks for the content to be shown inline. Use fileName(String) to let that place decide.

      To be called before the response is committed.

      If the fileName is null or 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
    • 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 with fileName(String) is kept.

      The header is inline even when the place where the handler is used asks for the content to be saved as a file. Use fileName(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

      public void inline(String fileName)
      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 inline even when the place where the handler is used asks for the content to be saved as a file. Use fileName(String) alone to let that place decide.

      To be called before the response is committed.

      If the fileName is null or blank, this behaves the same as calling inline(). 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

      public void setContentType(String contentType)
      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

      public Component getOwningComponent()
      Get owner Component for 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

      public Element 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

      public UI getUI()
      Get the current UI instance for this request that can be used to make asynchronous UI updates with UI.access(com.vaadin.flow.server.Command).
      Returns:
      Current UI instance