Gorkamorka
Gorkamorka

Reputation: 448

Open link to file in Vaadin

I'm trying to open a link to a file in a new window in Vaadin using the following code:

getWindow().open(new ExternalResource("./uploads/file.png"), "_blank");

I have the file stored in the webapp folder under /uploads/, hosted on a Tomcat server. But when I click the button which triggers the above code, a new window is opened with the previous window's content and with a URL pointing to the file, but no file download is triggered.

How do I open a link for downloading in a new window?

Upvotes: 0

Views: 7390

Answers (1)

eeq
eeq

Reputation: 2118

First, did you simply mean "../uploads/file.png"? (i.e. a resource outside the web application.)

You are using the ExternalResource, which basically means that you are opening a URL in the browser. The URL "./uploads/file.png" points to back to the application and you see the same window.

Instead you could be using FileResource to let the Vaadin application to serve the file. In this case, I suggest not to use relative paths as they may vary depending on the deployment server.

If you want to serve the file directly using Tomcat in the same web application context you should define in the web.xml that the "./uploads" does not map back to your application.

The easiest way to ensure this is to put the Vaadin application under "/application/*" (or similar) path. Without FileResource in the middle, this is more efficient approach. Downside is that the application URI changes and that is visible to the users.

Upvotes: 2

Related Questions