Class ResponseFile

java.lang.Object
com.axelor.rpc.ResponseFile

public final class ResponseFile extends Object
A file sent to the client, set with ActionResponse.setFile(ResponseFile).

The file content is copied to a pending export file, which the client downloads by token with the ws/files/data-export endpoint. A token is only valid for the user who created it.

 response.setFile(ResponseFile.preview(path, "invoice.pdf").title("Invoice"));
 response.setFile(ResponseFile.download(stream, "export.zip"));
 
  • Method Details

    • preview

      public static ResponseFile preview(Path path)
      Create a file the client opens in a new tab for preview if supported by the browser; otherwise, it downloads it.
      Parameters:
      path - the path to the file, absolute or relative to the temporary directory
      Returns:
      the file to preview, named after the given path
    • preview

      public static ResponseFile preview(Path path, @Nullable String fileName)
      Create a file the client opens in a new tab for preview if supported by the browser; otherwise, it downloads it.
      Parameters:
      path - the path to the file, absolute or relative to the temporary directory
      fileName - the name of the file, defaults to the name of the path
      Returns:
      the file to preview
    • preview

      public static ResponseFile preview(InputStream stream, String fileName)
      Create a file the client opens in a new tab for preview if supported by the browser; otherwise, it downloads it.
      Parameters:
      stream - the stream to the file
      fileName - the name of the file, whose extension tells whether it can be previewed
      Returns:
      the file to preview
      Throws:
      IllegalArgumentException - if the file name is blank
    • download

      public static ResponseFile download(Path path)
      Create a file the client downloads.
      Parameters:
      path - the path to the file, absolute or relative to the temporary directory
      Returns:
      the file to download, named after the given path
    • download

      public static ResponseFile download(Path path, @Nullable String fileName)
      Create a file the client downloads.
      Parameters:
      path - the path to the file, absolute or relative to the temporary directory
      fileName - the name of the downloaded file, defaults to the name of the path
      Returns:
      the file to download
    • download

      public static ResponseFile download(InputStream stream)
      Create a file the client downloads.
      Parameters:
      stream - the stream to the file
      Returns:
      the file to download, named after its pending export file
    • download

      public static ResponseFile download(InputStream stream, @Nullable String fileName)
      Create a file the client downloads.
      Parameters:
      stream - the stream to the file
      fileName - the name of the downloaded file
      Returns:
      the file to download
    • title

      public ResponseFile title(@Nullable String title)
      Set the title of the tab the file is previewed in.

      It is ignored if the file is downloaded.

      Parameters:
      title - the title of the tab, defaults to the file name
      Returns:
      this file
    • getFileName

      @Nullable public String getFileName()
      Get the name of the file.
      Returns:
      the name of the file, or null if a stream was given without a name
    • getTitle

      @Nullable public String getTitle()
      Get the title of the tab the file is previewed in.
      Returns:
      the title of the tab, or null to use the file name
    • isDownload

      public boolean isDownload()
      Whether the file was created to be downloaded, instead of previewed.

      A file created for preview is still downloaded if the browser cannot preview it.

      Returns:
      true if the file was created with download
    • export

      public Map<String,Object> export()
      Copy the file to a new pending export file, and describe it for the client.

      Each call creates a new pending export file with its own token, and reads the given stream to its end.

      Returns:
      the file token, name and format, as the client reads them
      Throws:
      IllegalArgumentException - if a relative path resolves outside the temporary directory
      UncheckedIOException - if the file cannot be read