Vaadin Flow下载代码仅适用于Chrome,但不适用于Firefox-如何同时支持两者? [英] Vaadin Flow download code works for Chrome but not for Firefox - How can I support both?

查看:100
本文介绍了Vaadin Flow下载代码仅适用于Chrome,但不适用于Firefox-如何同时支持两者?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码从Vaadin Flow(12.0.7)下载文件.

I have the following code to download a file from Vaadin Flow (12.0.7).

exportBtn.addClickListener(e -> {
toDownload = FileUtil.getLatestExport();

(toDownload != null) {
                StreamResource resource = new StreamResource(toDownload.getName(),
                        () -> FileUtil.getInputStreamForFile(toDownload));

                Element object = new Element("object");
                object.setAttribute("download", true);
                object.setAttribute("data", resource);

                Input name = new Input();
                UI.getCurrent().getElement().appendChild(name.getElement(), object);
   }
});

toDownload找到我要下载的文件.如果我单击Chrome中的按钮,则浏览器将下载我的文件;如果我单击Firefox中的按钮,则没有任何反应.我需要以哪种方式调整代码以支持Chrome和Firefox?

toDownload locates the file which I want to download. If I click the button from Chrome the browser downloads my file if I click the button from Firefox nothing happens. In what way do I need to adjust my code to support Chrome and Firefox?

我使用了此教程作为参考.

推荐答案

对于Vaadin Flow中的某些操作(例如,您有一个按钮,该按钮在下载文件之前有条件地显示一个对话框:

There is also a workaround for downloads triggered by some action in Vaadin Flow, e.g. you have a button that conditionally shows a dialog before downloading the file:

 Anchor hiddenDownloadLink = new Anchor(createYourStreamResource(), "Workaround");
 hiddenDownloadLink.setId("DownloadLinkWorkaround_" + System.currentTimeMillis());
 hiddenDownloadLink.getElement().setAttribute("style", "display: none");
 // TODO: add the link somehwere in your view
 UI.getCurrent().getPage().executeJs("document.getElementById('" + hiddenDownloadLink.getId().orElseThrow() + "').click();");

在FF,Chrome和Edge中进行了测试.解决方法是模拟点击锚点后触发下载.

Tested in FF, Chrome and Edge. The workaround simulates a click on an anchor that triggers the download.

这篇关于Vaadin Flow下载代码仅适用于Chrome,但不适用于Firefox-如何同时支持两者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆