下载生成的 HTML 文件
我有一个生成的 HTML 文件,我需要能够通过单击按钮来下载该文件(因此不会在新窗口中打开,仅显示此特定 html 文件的保存文件对话框)。
到目前为止我拥有的代码:
AVaadinApplication application = VaadinContext.getCurrentInstance().getVariable(VaadinContext.APPLICATION, AVaadinApplication.class);
StreamResource inputHTML = new StreamResource(
new StreamResource.StreamSource() {
private static final long serialVersionUID = 1L;
@Override
public InputStream getStream() {
return new ByteArrayInputStream(getHelpContentString(getLocale()).getBytes());
}
}, "help_" + getLocale() + ".html", application);
inputHTML.setCacheTime(0);
非常感谢任何帮助,因为我对此一直很头疼。
I have a generated HTML file which I need to be able to download at the click of a button (so not opening in a new window, just showing the save file dialog for this particular html file).
The code I have so far:
AVaadinApplication application = VaadinContext.getCurrentInstance().getVariable(VaadinContext.APPLICATION, AVaadinApplication.class);
StreamResource inputHTML = new StreamResource(
new StreamResource.StreamSource() {
private static final long serialVersionUID = 1L;
@Override
public InputStream getStream() {
return new ByteArrayInputStream(getHelpContentString(getLocale()).getBytes());
}
}, "help_" + getLocale() + ".html", application);
inputHTML.setCacheTime(0);
Any help is greatly appreciated as I've been breaking my head on this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将 HTML 封装在 Zip 文件中。据我所知,没有浏览器会尝试显示 Zip 文件,而是将其作为文件推出以“打开(在 Zip 存档程序中)或保存”。
Wrap the HTML in a Zip file. No browser I know will attempt to display a Zip file, but instead push it out as a file to 'Open (in a Zip archive program) or Save'.
您应该将结果的 HTTP
Content-Disposition
标头设置为附件。如何执行此操作取决于您与 Web 服务器交互的方式。以前的代码是否作为 servlet、游戏框架、cgi 等运行?You should set the HTTP
Content-Disposition
header of the result to attachment. How you do that depends on how you interact with the web server. Are yore code running as a servlet, in the play framwork, as a cgi, etc?