如何在没有按钮的情况下在vaadin中开始文件下载?
我知道创建如何在没有按钮的情况下在vaadin中开始文件下载?,vaadin,vaadin7,Vaadin,Vaadin7,我知道创建FileDownloader并使用按钮调用extend非常容易。但是如果没有按钮,如何开始下载 在我目前的具体情况下,我有一个组合框,我想发送给用户的文件是根据输入更改其值后生成的。文件应立即发送,无需等待再次单击。这容易做到吗 谢谢 拉斐尔我自己找到了解决办法。实际上是两个。 第一种方法使用不推荐使用的方法 javadoc提到了一些内存和安全问题,作为将其标记为不推荐的原因 在第二部分中,我尝试通过在DownloadComponent中注册资源来绕过这个不推荐的方法。如果瓦丁专家对
FileDownloader
并使用按钮调用extend非常容易。但是如果没有按钮
,如何开始下载
在我目前的具体情况下,我有一个组合框
,我想发送给用户的文件是根据输入更改其值后生成的。文件应立即发送,无需等待再次单击。这容易做到吗
谢谢
拉斐尔我自己找到了解决办法。实际上是两个。
第一种方法使用不推荐使用的方法
javadoc提到了一些内存和安全问题,作为将其标记为不推荐的原因
在第二部分中,我尝试通过在DownloadComponent中注册资源来绕过这个不推荐的方法。如果瓦丁专家对这个解决方案发表意见,我会很高兴
public class DownloadComponent extends CustomComponent implements ValueChangeListener {
private ComboBox cb = new ComboBox();
private static final String MYKEY = "download";
public DownloadComponent() {
cb.addValueChangeListener(this);
cb.setNewItemsAllowed(true);
cb.setImmediate(true);
cb.setNullSelectionAllowed(false);
setCompositionRoot(cb);
}
@Override
public void valueChange(ValueChangeEvent event) {
String val = (String) event.getProperty().getValue();
FileResource res = new FileResource(new File(val));
setResource(MYKEY, res);
ResourceReference rr = ResourceReference.create(res, this, MYKEY);
Page.getCurrent().open(rr.getURL(), null);
}
}
注意:我不允许用户在服务器上打开我的所有文件,您也不应该这样做。这只是为了演示。这是我的工作。它对我来说很有魅力。希望它能帮助你
创建一个按钮并通过Css隐藏它(而不是通过代码:button.setInvisible(false))
在主题中,添加此规则以隐藏下载InvisibleButton
:
.InvisibleButton {
display: none;
}
当用户单击菜单项时:将fileDownloader
扩展到downloadvisiblebutton
,然后通过JavaScript模拟单击downloadvisiblebutton
menuBar.addItem("Download", new MenuBar.Command() {
@Override
public void menuSelected(MenuBar.MenuItem selectedItem) {
FileDownloader fileDownloader = new FileDownloader(...);
fileDownloader.extend(downloadInvisibleButton);
//Simulate the click on downloadInvisibleButton by JavaScript
Page.getCurrent().getJavaScript()
.execute("document.getElementById('DownloadButtonId').click();");
}
});
这不起作用,因为每次单击同一项都会为invisibleButton添加一个扩展,因此它会第一次触发一个dowload,但第二次触发两个dowload…依此类推..Lorenzo:在调用extend()之前,您需要删除按钮的所有旧扩展名。这些解决方案是否在新选项卡或新弹出窗口中打开文件?您可以通过在Page.open()中设置windowName参数来控制行为。请参阅:当然,但它不会打开“另存为”文件浏览器可能是浏览器阻止了弹出窗口。您如何定义文件下载到哪里?隐藏按钮的问题是,这在使用iOS的设备上不起作用。我宁愿使用MenuBar和MenuItem,但这还不受Vaadin的支持-请参阅。使用MenuBar和MenuItem时建议的解决方法将导致使用隐藏按钮。。。
.InvisibleButton {
display: none;
}
menuBar.addItem("Download", new MenuBar.Command() {
@Override
public void menuSelected(MenuBar.MenuItem selectedItem) {
FileDownloader fileDownloader = new FileDownloader(...);
fileDownloader.extend(downloadInvisibleButton);
//Simulate the click on downloadInvisibleButton by JavaScript
Page.getCurrent().getJavaScript()
.execute("document.getElementById('DownloadButtonId').click();");
}
});