问题描述
我希望在zipManager.makeZip()被执行时显示ajaxStatus,直到下载开始。
如果ajax = false,文件下载工作,但ajaxStatus不显示。
如果ajax = true,则显示ajaxStatus,但下载不起作用。
I would like to display ajaxStatus while zipManager.makeZip() is executed... until the download starts.if ajax=false, the file download works but ajaxStatus not displayed.if ajax=true, ajaxStatus is displayed but download not working!
任何想法如何使ajaxStatus和fileDownload一起工作。
Any idea how to make the ajaxStatus and fileDownload work together.
提前感谢
kem
<h:form id="form">
<p:commandLink id="download" value="Download"
onstart="showStatus()" oncomplete="hideStatus()"
actionListener="#{zipManager.makeZip()}">
<p:fileDownload value="#{zipManager.zip}"/>
</p:commandLink>
</h:form>
<p:ajaxStatus id="status" widgetVar="st" style="position:fixed;right:50%;bottom:50%">
<f:facet name="start">
<p:graphicImage value="images/wait.gif" />
</f:facet>
</p:ajaxStatus>
推荐答案
这是因为下载不会发生ajax请求。
That's because the download doesn't take place by an ajax request.
这是因为下载不能通过ajax请求进行。 JS / Ajax将成功地检索该文件,但不知道如何处理它。没有办法强制使用JS另存为对话框。没有办法使用JS访问本地磁盘文件系统(否则这将是一个巨大的安全漏洞)。
That's because the download can't take place by an ajax request. JS/Ajax will successfully retrieve the file, but have no idea how to deal with it. There's no way to force a Save As dialogue with JS. There's no way to access the local disk file system with JS (it would otherwise have been a huge security breach).
使用PrimeFaces提供 PrimeFaces.monitorDownload()
JS函数。 ,这是下面提供的参考(特别注意文件下载命令按钮的 onclick
属性):
Use the PrimeFaces-provided PrimeFaces.monitorDownload()
JS function. A complete example can be found on their own <p:fileDownload>
showcase page which is copypasted below for reference (note particularly the onclick
attribute of the file download command button):
<p:dialog modal="true" widgetVar="statusDialog" header="Status"
draggable="false" closable="false" resizable="false">
<p:graphicImage value="/design/ajaxloadingbar.gif" />
</p:dialog>
<h:form id="form">
<p:commandButton id="downloadLink" value="Download" ajax="false"
onclick="PrimeFaces.monitorDownload(start, stop)"
icon="ui-icon-arrowthichk-s">
<p:fileDownload value="#{fileDownloadController.file}" />
</p:commandButton>
</h:form>
<script type="text/javascript">
function start() {
statusDialog.show();
}
function stop() {
statusDialog.hide();
}
</script>
可以通过将命令链接更改为如下:
This can be applied in your particular case by changing the command link as follows:
<p:commandLink id="download" value="Download" ajax="false"
onclick="PrimeFaces.monitorDownload(showStatus, hideStatus)"
actionListener="#{zipManager.makeZip()}">
并替换< p:ajaxStatus>
通过一个简单的< p:dialog>
,如展示示例所示。
and replacing the <p:ajaxStatus>
by a simple <p:dialog>
as demonstrated in showcase example.
这一切都在幕后使用一个特殊的cookie,短时间间隔由JS(每100ms左右)轮询。在创建文件下载响应期间,将在其标题上设置一个特殊的cookie。一旦文件下载响应标头到达浏览器,则cookie将在浏览器中设置。当JS轮询器在浏览器cookie空间中找到它时,它会关闭进度。
This all works behind the scenes with a special cookie which is polled at short intervals by JS (every 100ms or so). During creating of the file download response, a special cookie will be set on its headers. Once the file download response headers arrives at the browser, then the cookie is set in the browser. When the JS poller finds it in the browser cookie space, then it closes the progess.
这篇关于在p:fileDownload开始时显示状态,完成后隐藏状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!