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

      public void setFileName(String fileName)
      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 fileName is null, the Content-Disposition header won't be set.

      Parameters:
      fileName - the name to be assigned to the file
    • 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