PrimeFaces FileUpload测量上传时间



是否有办法测量文件上传所需的时间?我需要测量它所花费的时间,如果它是>2分钟我想通知用户,上传(不是文件的处理)将花费比预期更长的时间

<p:fileUpload listener="#{fileUploadBean.handleFileUpload}"
value="#{fileUploadBean.uploadedFile}"
invalidFileMessage="File invalid.Please choose a XLSX/CSV/ZIP file"
invalidSizeMessage="The size of your file should not exceed 200MB"
sizeLimit="2000000000" showButtons="true" 
accept=".csv,.xlsx,.CSV,.XLSX,.zip,.ZIP"
allowTypes="/(.|/)(csv|xlsx|CSV|XLSX|zip|ZIP)$/"
fileLimit="1" dragDropSupport="true" uploadLabel="Upload"
update="@form :uploadData" 
id="fileUpload" />

只是提到文件上传工作得很好。没有错误,我唯一的问题是如何做定时器。

如果你想要一个基本的解决方案,你可以在JS中使用setTimeout函数(你可以把这个方法看作是一种设置定时器以在特定时间运行JavaScript代码的方法),它在X毫秒内触发一个remoteCommand,如下所示(我已经用100毫秒测试了它是否有效):

<h:form id="formFile" enctype="multipart/form-data">
<p:growl id="growl" showDetail="true" sticky="true"/>
<p:remoteCommand id="showTimerMessage" name="showTimerMessage" update="growl" actionListener="#{growlView.showTimer}"/>
<p:fileUpload mode="simple"
value="#{helloWorld.file}"
update="growl"
fileLimit="1"
id="fileUpload" />
<p:commandButton value="Submit" ajax="false"   onclick="my_timer();"
oncomplete="skipMessage();"
action="#{helloWorld.upload}" styleClass="p-mt-3 ui-button-outlined p-d-block"/>
</h:form>
<script type="text/javascript">
let showMessage = true;
function my_timer() {
setTimeout( () => {
if(showMessage) showTimerMessage();
}, 100);
}
function skipMessage() {
showMessage = false;
}
</script>

和GrowlView后端:

@Named @RequestScoped
public class GrowlView implements Serializable {
public void addMessage(FacesMessage.Severity severity, String summary, String detail) {
FacesContext.getCurrentInstance().
addMessage(null, new FacesMessage(severity, summary, detail));
}
public void showTimer() {
addMessage(FacesMessage.SEVERITY_INFO, "Timer message", "The upload (not the processing of the file) will take longer than expected");
}}

如果文件上传在<100毫秒,showTimerMessage remoteCommand没有被触发,并且该命令不会显示您的消息。通过这种方式,您可以避免使用外部依赖项或库。

相关内容

  • 没有找到相关文章

最新更新