如何检测 Vaadin FileDownloader 何时成功或失败 [英] How to detect when Vaadin FileDownloader succeeds or fails

查看:21
本文介绍了如何检测 Vaadin FileDownloader 何时成功或失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 Vaadin 7 代码可​​以让用户选择下载文件:

I have Vaadin 7 code to give the user an option to download a file:

    Button btnDownloadResults = new Button("Download Results", FontAwesome.CLOUD_DOWNLOAD);
    resource = new StreamResource(new MyStreamResource(), suggestedSaveAsFilename);
    new FileDownloader(resource).extend(btnDownloadResults);

我想在下载成功时触发代码,或者即使下载设法开始.其用途包括关闭窗口、启动进度微调器或增加下载计数.

I would like to trigger code when the download has succeeded, or even if the download manages to start. Uses for this include closing a window, starting a progress spinner, or incrementing a download count.

与 Vaadin Upload 组件不同,FileDownloader 没有任何侦听器,用于发现文件下载何时失败、成功或开始.

Unlike the Vaadin Upload component, the FileDownloader does not have any listeners for finding out when a file download fails, succeeds, or starts.

这是我的 StreamResouce 子类的简化版本:

Here is a simplified version of my StreamResouce subclass:

public class MyStreamResource implements StreamSource {
@Override
public InputStream getStream() {
    String filename = /* code to determine the filename */;

    try {
        final File results = new File(FilenameUtils.normalize(filename));
        return new FileInputStream(results);
    } catch (FileNotFoundException fnfe) {
        String errorMsg = "Cannot download results. Try again later, or contact your sysadmin.";
        Utilities.showError(errorMsg);

        return null;
    } catch (Exception e) {
        Utilities.logAndShowException(e);
        return null;
    }
}
}

请注意,getStream 方法在用户被提示保存文件的位置之前就返回了(他们可以选择取消.)所以我无法从该方法内部触发任何内容.

Note that the getStream method returns before the user has even been prompted where to save the file (which they can choose to cancel.) So I can't trigger anything from inside that method.

我得到的一个建议是将 FileDownloader 子类化如下:

One suggestion I got was to subclass the FileDownloader as follows:

FileDownloader fileDownloader = new FileDownloader(fileInputStream) {
    private static final long serialVersionUID = -4584979099145066535L;
    @Override
    public boolean handleConnectorRequest(VaadinRequest request, VaadinResponse response, String path) throws IOException {
        boolean result = super.handleConnectorRequest(request, response, path);
        if (result) {
            /* YOUR LOGIC GOES HERE */
        }
        return result;
    }
} ;

同样,这触发得太快了(并且布尔结果始终为真,即使我的 StreamSource 返回 null.)

Again, this fires too soon (and the boolean result is always true, even if my StreamSource returns null.)

有什么建议吗?

推荐答案

经过更多研究,我相信答案是没有简单的方法可以从 FileDownloader 获取此信息.

After more research I believe the answer is that there is no simple way to get this information from the FileDownloader.

困难似乎是 FileDownloader 设计方式的结果.来自 FileDownloader 文档:应在用户单击例如 Button 时直接开始下载,而无需通过服务器端单击侦听器,以避免在某些浏览器中触发安全警告."

The difficulty appears to be a consequence of the way the FileDownloader is designed. From the FileDownloader docs: "Download should be started directly when the user clicks e.g. a Button without going through a server-side click listener to avoid triggering security warnings in some browsers."

因为没有返回到 Web 服务器的往返,所以在下载失败、启动或成功时没有地方可以响应.

Because there is no round-trip back to the web server, there is no place to respond when the download fails, starts, or succeeds.

一些模糊的(可能是坏的)解决方法的想法:

Some vague (and possibly bad) ideas for a workaround:

  • 让 JS 向网络发布某种异步通知服务器,让它知道发生了什么.(使用 JMS 还是 Ajax?)
  • 如果后端有某种与传输文件有关的活动进程,它会会知道转移发生的时间.

但简短的回答似乎是 Vaadin 没有内置方法可以做到这一点.

But the short answer seems to be there is no built-in way in Vaadin to do it.

这篇关于如何检测 Vaadin FileDownloader 何时成功或失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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