Browse Source

Document how to handle cancelled file downloads. (#11079)

tags/8.6.0.alpha2
Ilia Motornyi 5 years ago
parent
commit
156121c769
No account linked to committer's email address

+ 26
- 2
documentation/articles/LettingTheUserDownloadAFile.asciidoc View File

@@ -62,8 +62,7 @@ along with the file to ensure the browser doesn't try to open the file
even if it's is a file type that the browser knows how to deal with.

[[lazily-determine-the-content-and-the-name-of-the-file-being-server]]
Lazily determine the content and the name of the file being server
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
==== Lazily determine the content and the name of the file being server

One can lazily determine the content of the file using a
`StreamResource`. Yet the name of the file that is going to be
@@ -110,3 +109,28 @@ public class OnDemandFileDownloader extends FileDownloader {
}
}
....

[[lazily-determine-the-content-and-the-name-of-the-file-being-server]]
==== Cancelled downloads

Since downloadable files may be quite big, and the download process may take time, the user might decide to
cancel the download process. In this case `IOException` may be thrown by the web server. That
does not mean something went wrong with the application, but the user pressed `Cancel` button during download. To prevent the exception to be logged, you can catch and ignore it as here:

```java
public class IgnoreCancelDownloader extends FileDownloader {

...

@Override
public boolean handleConnectorRequest(final VaadinRequest request, final VaadinResponse response, final String path) {
try {
return super.handleConnectorRequest(request, response, path);
} catch (final IOException ignored) {
return true;
}
}
}

```
Note that the exception is a sublclass of `IOException`, but the particular class depends on the web container.

+ 8
- 3
server/src/main/java/com/vaadin/server/FileDownloader.java View File

@@ -81,7 +81,6 @@ public class FileDownloader extends AbstractExtension {
*
* @param eventTrigger
* the trigger to attach this extension to
*
* @since 8.4
*/
public void extend(EventTrigger eventTrigger) {
@@ -132,16 +131,22 @@ public class FileDownloader extends AbstractExtension {
/**
* Checks whether the content type should be overridden.
*
* @see #setOverrideContentType(boolean)
*
* @return <code>true</code> if the content type will be overridden when
* possible; <code>false</code> if the original content type will be
* used.
* @see #setOverrideContentType(boolean)
*/
public boolean isOverrideContentType() {
return overrideContentType;
}

/**
* {@inheritDoc}
*
* @throws IOException
* if something goes wrong with the download or the user
* cancelled the file download process.
*/
@Override
public boolean handleConnectorRequest(VaadinRequest request,
VaadinResponse response, String path) throws IOException {

Loading…
Cancel
Save