如何将 Wicket 的 DownloadLink 与动态生成的文件一起使用?
DownloadLink
对于创建用于下载文件的按钮/链接来说非常好用且方便,沿着这些思路:
add(new DownloadLink("downloadButton", getReportFile(), "report.pdf"));
但是
<input type="button" wicket:id="downloadButton" value="Download" />
,我想仅在以下情况下触发文件的生成以下载:单击按钮/链接。换句话说,点击后,我将调用一个方法来生成文件(在我们的例子中是 Pentaho 报告),将其放在临时位置并返回一个指向它的 File
。然后我会告诉 DownloadLink
使用该 File
。问题是,这是否可能?
目前我们有类似下面的代码,它可以工作,但我感兴趣的是是否可以使用 DownloadLink
来代替。
add(new Link<Void>("downloadButton") {
@Override
public void onClick() {
IResourceStream resourceStream = new AbstractResourceStreamWriter() {
@Override
public void write(OutputStream output) {
try {
reportService.generateReport(output, report);
} catch (IOException e) {
// ...
}
}
@Override
public String getContentType() {
return CONTENT_TYPE_PDF;
}
};
getRequestCycle()
.setRequestTarget(new ResourceStreamRequestTarget(resourceStream)
.setFileName("report.pdf"));
}
});
(Wicket 1.4.18,如果有影响的话。)
DownloadLink
is nice and handy for creating a button/link for downloading a file, along these lines:
add(new DownloadLink("downloadButton", getReportFile(), "report.pdf"));
and
<input type="button" wicket:id="downloadButton" value="Download" />
However, I would like to trigger the generation of the file to download only when the button/link is clicked. In other words, upon click, I'd call a method that generates the file (a Pentaho report in our case), puts it in a temp place and returns a File
pointing to it. Then I'd tell the DownloadLink
to use that File
. Question is, is this possible somehow?
Currently we have something like the code below, which works, but I'm interested in whether DownloadLink
could be used instead.
add(new Link<Void>("downloadButton") {
@Override
public void onClick() {
IResourceStream resourceStream = new AbstractResourceStreamWriter() {
@Override
public void write(OutputStream output) {
try {
reportService.generateReport(output, report);
} catch (IOException e) {
// ...
}
}
@Override
public String getContentType() {
return CONTENT_TYPE_PDF;
}
};
getRequestCycle()
.setRequestTarget(new ResourceStreamRequestTarget(resourceStream)
.setFileName("report.pdf"));
}
});
(Wicket 1.4.18, if it makes a difference.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能使用以
Model
作为参数的构造函数吗?并使Model
在其getObject()
中生成File
。 LoadableDetachableModel 是一个不错的选择,因为load()
以及文件生成只会被调用一次。如果每次单击链接时都要重新生成文件,请使用
DownloadLink.setDeleteAfterDownload(true)
确保文件一旦被自动删除服务。我不使用1.4,但1.3中的源代码显示,
File
是通过onClick()
中的getModelObject()
检索的>Link
的方法。DownloadLink.onClick()
Can't you use the constructor that takes a
Model
as argument? And make theModel
generate theFile
in itsgetObject()
. ALoadableDetachableModel
is a good choice, given thatload()
, and therefore file generation, will be invoked only once.If the file is to be freshly generated every time the link is clicked, use
DownloadLink.setDeleteAfterDownload(true)
to ensure the File is automatically deleted once it is served.I don't use 1.4, but the source code in 1.3 shows that the
File
is retrieved by means ofgetModelObject()
in theonClick()
method of theLink
.Source code of
DownloadLink.onClick()
请改用 org.apache.wicket.markup.html.link.ResourceLink。
Use org.apache.wicket.markup.html.link.ResourceLink instead.