JS 功能未通过 PrimeFaces.monitorDownload 触发 [英] JS function not triggered with PrimeFaces.monitorDownload

查看:14
本文介绍了JS 功能未通过 PrimeFaces.monitorDownload 触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Primefaces 4.0 进行文件下载.我只想在下载完成时触发一个 JS 函数,但似乎不起作用(在 Firefox 和 Google Chrome 中尝试过).我的测试用例看起来类似于在 PF 文档中所做的:>

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"xmlns:f="http://java.sun.com/jsf/core"xmlns:h="http://java.sun.com/jsf/html"xmlns:ui="http://java.sun.com/jsf/facelets"xmlns:p="http://primefaces.org/ui"><h:头/><h:body><script type="text/javascript">函数开始消息(){alert("下载开始了!");}函数完成消息(){alert("下载完成!");}<h:形式><p:commandButton value="下载"ajax="false"icon="ui-icon-arrowreturnthick-1-s"onclick="PrimeFaces.monitorDownload(startMessage, finishMessage)"><p:fileDownload value="#{bean.file}"/></p:commandButton></h:form></h:body>

@ManagedBean@ViewScoped公共类 Bean 实现了 Serializable {公共流内容 getFile() {返回新的 DefaultStreamedContent(new ByteArrayInputStream(new byte[0]));}}

警报会在下载开始时触发,但不会在下载完成时触发.其他人可以试试吗?

解决方案

这是一个错误

一>.

主要错误在org.primefaces.component.filedownload包的FileDownloadActionListener.

第 65 行

externalContext.addResponseCookie(Constants.DOWNLOAD_COOKIE, "true", Collections.emptyMap());

Constants.DOWNLOAD_COOKIE 是primefaces.download",它永远不会与响应一起发送.

这会导致 PrimeFaces.monitorDownload 的 Interval 永远不会调用停止函数,因为 cookie 永远不会被写入.

I'm working in file downloading with Primefaces 4.0. I just want to trigger a JS function when download completes, but seems not to work (tried in Firefox and Google Chrome). My test case looks similar to what's done in the PF docs:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">
<h:head />

<h:body>
    <script type="text/javascript">
        function startMessage() {
            alert("Download started!");
        }
        function finishMessage() {
            alert("Download finished!");
        }
    </script>
    <h:form>
        <p:commandButton value="Download" ajax="false"
            icon="ui-icon-arrowreturnthick-1-s"
            onclick="PrimeFaces.monitorDownload(startMessage, finishMessage)">
            <p:fileDownload value="#{bean.file}" />
        </p:commandButton>
    </h:form>
</h:body>
</html>

@ManagedBean
@ViewScoped
public class Bean implements Serializable {

    public StreamedContent getFile() {
        return new DefaultStreamedContent(new ByteArrayInputStream(new byte[0]));
    }

}

The alert is triggered when download starts, but not when download finishes. Could anyone else give it a try?

解决方案

it's a bug.

The main bug is in FileDownloadActionListener of org.primefaces.component.filedownload package.

Line 65

externalContext.addResponseCookie(Constants.DOWNLOAD_COOKIE, "true", Collections.<String, Object>emptyMap());

The Constants.DOWNLOAD_COOKIE is "primefaces.download", and it's never sent with the response.

That would cause PrimeFaces.monitorDownload's Interval to never call the stop function, since the cookie is never written.

这篇关于JS 功能未通过 PrimeFaces.monitorDownload 触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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