File download
In Chapter 2, Using Input Components and Forms – Time to Listen to Users, we learned how to upload files. Now we're going to learn how to download files. Let's say we have a PDF file and want users to download it by clicking a button. Doing it is quite easy. First a PDF file is a Resource
, right? Suppose the file is in the classpath:
ClassResource resource = new ClassResource("enterprise-app.pdf");
We also need a Button
:
Button button = new Button("Download the PDF");
button.setStyleName(BaseTheme.BUTTON_LINK);
Tip
Use button.setStyleName(BaseTheme.BUTTON_LINK)
to render the button like a standard link. Next chapter we'll learn more about styles.
The following two lines will make the rest of the job:
FileDownloader downloader = new FileDownloader(resource); downloader.extend(button);
FileDownloader
is an Extension
. En Extension
is an interface that allows adding functionality to a component. This particular extension starts a download when the extended component is clicked. Take a...