PrimeFaces.monitorDownload未触发JS函数 [英] JS function not triggered with PrimeFaces.monitorDownload
问题描述
我正在使用 Primefaces 4.0 下载文件.我只想在下载完成时触发JS函数,但似乎不起作用(在Firefox和Google Chrome中尝试过).我的测试用例看起来与 PF文档中的操作类似:>
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?
推荐答案
这是一个错误.
主要错误位于org.primefaces.component.filedownload
程序包的FileDownloadActionListener
中.
The main bug is in FileDownloadActionListener
of org.primefaces.component.filedownload
package.
第65行
externalContext.addResponseCookie(Constants.DOWNLOAD_COOKIE, "true", Collections.<String, Object>emptyMap());
Constants.DOWNLOAD_COOKIE
是"primefaces.download",并且永远不会随响应一起发送.
The Constants.DOWNLOAD_COOKIE
is "primefaces.download", and it's never sent with the response.
这将导致PrimeFaces.monitorDownload
的时间间隔从不调用stop函数,因为从不写入cookie.
That would cause PrimeFaces.monitorDownload
's Interval to never call the stop function, since the cookie is never written.
这篇关于PrimeFaces.monitorDownload未触发JS函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!