Class FileDownloadHandler
- All Implemented Interfaces:
DownloadHandler,ElementRequestHandler,Serializable
Byte range requests, which media players use to seek, are answered with the requested part of the file.
- Since:
- 24.8
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionFileDownloadHandler(File file) Create a download handler for given file.FileDownloadHandler(File file, String fileNameOverride) Create a download handler for given file. -
Method Summary
Modifier and TypeMethodDescriptionprotected TransferContextgetTransferContext(DownloadEvent transferEvent) This method is used to get the transfer context from the transfer events (e.g.Optional URL postfix allows appending an application-controlled string, e.g. the logical name of the target file, to the end of the otherwise random-looking download URL.voidhandleDownloadRequest(DownloadEvent downloadEvent) Method that is called when the client wants to download from the url stored for this specific handler registration.booleanReturns whether this handler answers byte range requests, which media players use to seek and browsers use to resume a download.Methods inherited from class com.vaadin.flow.server.streams.AbstractDownloadHandler
getContentType, getContentType, inline, isInline, resolveContentType, withContentTypeMethods inherited from class com.vaadin.flow.server.streams.TransferProgressAwareHandler
addTransferProgressListener, getListeners, notifyError, onProgress, onProgress, onProgress, onProgress, setTransferUI, uiAccess, whenComplete, whenComplete, whenStart, whenStartMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.vaadin.flow.server.streams.DownloadHandler
allowDisabled, handleRequestMethods inherited from interface com.vaadin.flow.server.streams.ElementRequestHandler
getDisabledUpdateMode, isAllowInert
-
Constructor Details
-
FileDownloadHandler
Create a download handler for given file. Url postfix will be used asfile.getName().The downloaded file name and download URL postfix will be set to
file.getName(). If you want to use a different file name, useFileDownloadHandler(File, String)instead.- Parameters:
file- file to download
-
FileDownloadHandler
Create a download handler for given file.The downloaded file fileNameOverride and download URL postfix will be set to
fileNameOverride.- Parameters:
file- file to downloadfileNameOverride- used as a downloaded file name (overridesfile.getName()) and also as a download request URL postfix, e.g./VAADIN/dynamic/resource/0/5298ee8b-9686-4a5a-ae1d-b38c62767d6a/my-file.txt
-
-
Method Details
-
handleDownloadRequest
Description copied from interface:DownloadHandlerMethod that is called when the client wants to download from the url stored for this specific handler registration.Once this method has been called, the download is no longer bound to the owner component's lifecycle and is not interrupted by detaching the owner or by closing the browser tab, see the
class-level documentation.- Parameters:
downloadEvent- download event containing the necessary data for writing the response- Throws:
IOException- if an IO error occurred during download
-
isRangeRequestsEnabled
public boolean isRangeRequestsEnabled()Returns whether this handler answers byte range requests, which media players use to seek and browsers use to resume a download. Safari does not play audio or video from a handler that does not answer them.A handler that does not answer them sends the whole content for every request. A handler that does may still send the whole content for a particular response, for example when the length of its content is not known. The default implementation returns
false.In development mode, a built-in handler logs a warning the first time that a handler of its class serves the whole content for a byte range request, for example because ranges are not enabled or the length of the content is unknown.
Range requests are always answered for a file.
- Returns:
trueif the handler is configured to answer byte range requests
-
getUrlPostfix
Description copied from interface:ElementRequestHandlerOptional URL postfix allows appending an application-controlled string, e.g. the logical name of the target file, to the end of the otherwise random-looking download URL. If defined, requests that would otherwise be routable are still rejected if the postfix is missing or invalid. Postfix changes the last segment in the resource url.- Returns:
- String optional URL postfix, or
nullfor "".
-
getTransferContext
Description copied from class:TransferProgressAwareHandlerThis method is used to get the transfer context from the transfer events (e.g.DownloadEvent).- Overrides:
getTransferContextin classAbstractDownloadHandler<FileDownloadHandler>- Parameters:
transferEvent- the transfer event- Returns:
- the transfer context
-