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); ...

Get Vaadin 7 UI Design By Example Beginner's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.