简体   繁体   English

在Vaadin中打开文件链接

[英]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: 我正在尝试使用以下代码在Vaadin的新窗口中打开指向文件的链接:

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

I have the file stored in the webapp folder under /uploads/, hosted on a Tomcat server. 我将文件存储在/ uploads /下的webapp文件夹中,托管在Tomcat服务器上。 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. 但是,当我单击触发上述代码的按钮时,将打开一个新窗口,其中包含上一个窗口的内容以及指向该文件的URL,但不会触发文件下载。

How do I open a link for downloading in a new window? 如何在新窗口中打开下载链接?

First, did you simply mean "../uploads/file.png"? 首先,你的意思是“../uploads/file.png”吗? (ie a resource outside the web application.) (即Web应用程序之外的资源。)

You are using the ExternalResource , which basically means that you are opening a URL in the browser. 您正在使用ExternalResource ,这基本上意味着您在浏览器中打开一个URL。 The URL "./uploads/file.png" points to back to the application and you see the same window. URL“./uploads/file.png”指向应用程序,您会看到相同的窗口。

Instead you could be using FileResource to let the Vaadin application to serve the file. 相反,您可以使用FileResource让Vaadin应用程序为该文件提供服务。 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. 如果你想为应该在的“./uploads” 映射回您的应用程序的web.xml定义相同的Web应用程序上下文 直接使用Tomcat的文件。

The easiest way to ensure this is to put the Vaadin application under "/application/*" (or similar) path. 确保这一点的最简单方法是将Vaadin应用程序置于“/ application / *”(或类似)路径下。 Without FileResource in the middle, this is more efficient approach. 如果没有FileResource,这是更有效的方法。 Downside is that the application URI changes and that is visible to the users. 缺点是应用程序URI发生变化,用户可以看到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM