Vaadin 7 - 即时下载动态创建的文件 [英] Vaadin 7 - instant download of dynamically created file
问题描述
我的场景是:使用 Vaadin 7.7.10,我点击生成的文件,我想在生成后下载.
My scenario is: using Vaadin 7.7.10, I have on click generated file, that I want to download once it is generated.
我有生成器方法,它返回byte[]
(如果需要,也可以很容易地修改为任何OutputStream
),然后我创建StreamResource
> 从它.所以我已经准备好下载资源了,但现在怎么办?
I have generator method, that returns byte[]
(could be also easily modified to any OutputStream
if needed), then I create StreamResource
from it. So I have the resource ready to be downloaded, but now what?
我相信不能使用 FileDownloader
的方法,因为在实际单击扩展按钮之前每次显示页面时我都必须生成文件(而且我不想这样做,因为这是非常昂贵的操作,只能在需要时使用).
I believe can't use approach with FileDownloader
, because there I would have to generate the file every time the page is displayed prior to actual click on extended button (and I don't want to do that, as it is quite expensive operation which will shall only be used on demand).
我可以使用 Page.getCurrent().open(streamResource, null, false)
达到预期的效果,但是 .open
使用 Resource
参数自 Vaadin 7 起已弃用,所以我不想依赖它...
I was able to achieve the desired effect using Page.getCurrent().open(streamResource, null, false)
, but .open
with Resource
argument is deprecated since Vaadin 7, so I don't wanna rely on it...
是否有任何解决方法或其他方法?
Is there any workaround or another approach to this?
推荐答案
Vaadin FileDownloader
可能仍然能够通过一些自定义来处理这个问题.然而,我曾经用不同的方法创建了一个报告系统.这个想法是使用组件 BrowserFrame
将 StreamResource
作为其构造函数参数.
Vaadin FileDownloader
might be still capable to handle this with some customization. However I once created a reporting system with a different approach. The idea is to use component BrowserFrame
that takes StreamResource
as its constructor param.
文件生成开始 - 例如 - 通过单击按钮.当生成结束时,它会生成一个 byte[]
.从 byte[]
创建一个 StreamSource
,然后用于创建 StreamResouce
,然后用作 BrowserFrame
的内容>.
File generation is started - for example - by clicking a button. When generation finalizes it results in to a byte[]
. From byte[]
is created a StreamSource
which then is used to create StreamResouce
which is then used as contents of BrowserFrame
.
最后这个 BrowserFrame
被添加到 ui 某处,导致浏览器做出反应.
Finally this BrowserFrame
is added to the ui somewhere which causes browser to react.
就我而言,需要生成参数化的 PDF 报告,用户可以先预览这些报告,然后在需要时下载.我有一个提供 byte[]
的 PDF 生成器.浏览器从文件扩展名 .pdf
识别类型并打开 PDF 插件进行预览 &下载.
In my case there was a need to generate parameterized PDF reports which user could first preview and then download if in need. I had a PDF generator that provided the byte[]
. Browser recognized type from file name extension .pdf
and opened PDF plugin for preview & download.
您可以将任何二进制内容放入 byte[]
用于构造 BrowserFrame
并设置适当的文件扩展名.
You can put any binary stuff to byte[]
used to construct BrowserFrame
and set appropriate file name extension.
您还可以在您的 ui 中使用一些 Layout
,例如,在后台生成文件时具有进度指示器,并且 BrowserFrame
组件位于其中文件准备好后附上.
You can also have some Layout
in your ui that - for example - has a progress indicator while file is generating in the background and in to which the BrowserFrame
component is attached when file is ready.
这篇关于Vaadin 7 - 即时下载动态创建的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!