在文件下载后执行页面导航 [英] Perform page navigation after a file download

查看:85
本文介绍了在文件下载后执行页面导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要找到一种在生成文件下载后执行页面导航的方法。到目前为止,我已经准备好了文件下载:

I need to find a way to perform a page navigation after generating a file download. So far, I've got the file download ready and working:

FileInputStream stream = new FileInputStream(file);
        FacesContext fc = FacesContext.getCurrentInstance();
        ExternalContext ec = fc.getExternalContext();
        ec.responseReset();
        ec.setResponseContentType("application/octet-stream");
        ec.setResponseHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");

        OutputStream out = ec.getResponseOutputStream();

        byte[] outputByte = new byte[4096];

        while(stream.read(outputByte, 0, 4096) != -1)
        {
                out.write(outputByte, 0, 4096);
        }
        stream.close();
        out.flush();
        out.close();
        fc.responseComplete();

到目前为止,我已经尝试从ExternalContext重定向,但是我得到一个IllegalStateException。 >

So far, I've try redirecting from the ExternalContext afterwards but i get an IllegalStateException.

ec.redirect(url)

我还尝试将所有以前的代码包装在一个字符串方法中,返回最后导航的页面。

I've also tried wrapping all the previous code in a string method that returns the page to be navigated at the end. That didn't work either.

任何建议?

推荐答案

您不能返回2个请求的回复。您只能返回1个请求的响应。文件下载作为一个响应计数,重定向计为另一个响应。

You can't return 2 responses to 1 request. You can return only 1 response to 1 request. A file download counts as one response and a redirect counts as another response.

最好的方法是返回响应,以某种方式自动启动新的请求。然后可以将第二个响应返回到此自动发起的请求。 JavaScript对于这样的功能非常有帮助,像 window.location (在当前窗口中触发一个新请求), window.open()(在新窗口中触发一个新请求)和 form.submit()(提交POST表单)。

Your best bet is to return a response which automatically initiates a new request in some way. The second response can then be returned to this automatically initiated request. JavaScript is very helpful in this with functions like window.location (to fire a new request in current window), window.open() (to fire a new request in a new window), and form.submit() (to submit a POST form).

最简单的方法是重定向到目标页面,其中一些JavaScript被有条件地呈现(并立即执行),其又通过例如 window.location form.submit()。如果下载本身已经设置为附件,则 window.open()是不合适的。请注意,这种方法不会在用户保存文件下载之后重定向,这是不可能的,因为当保存下载的最后一位时,没有客户端事件挂起。相反,首先执行重定向,然后执行文件下载。

The simplest way would be to redirect to the target page wherein some JavaScript is conditionally rendered (and immediately executed) which in turn triggers the file download by e.g. window.location or form.submit(). The window.open() is inappropriate if the download itself is already set as an attachment. Note that this approach doesn't redirect after the user has saved the file download, this is plain impossible as there's no client side event to hook on when the last bit of the download is saved. Instead, the redirect is first performed and then the file download.

如果您在这方面真的很难,那么您可以随时创建一个applet或webstart应用程序完全管理下载文件的保存,以便在保存最后一位后,您可以轻松执行重定向。然而,这是IMO简单的笨拙(甚至可能对UX的不利;例如我个人在几个网站上讨厌这种下载管理员)。

If you've really a hard head in this, then you can always create an applet or webstart application which fully manages the saving of the downloaded file, so that you can easily perform a redirect after the last bit is saved. However, this is IMO plain clumsy (and even potentially bad for UX; for instance I personally hate this kind of "download managers" on several websites).

这篇关于在文件下载后执行页面导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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