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 = new Button("Download the PDF"); button.setStyleName(BaseTheme.BUTTON_LINK);
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); ...