【问题标题】:PrimeFaces FileUpload measure the time of uploadPrimeFaces FileUpload 测量上传时间
【发布时间】:2021-10-21 08:27:44
【问题描述】:

有没有办法测量上传文件所需的时间。我需要测量它所花费的时间,如果超过 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" />

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

【问题讨论】:

标签: jsf primefaces


【解决方案1】:

如果你想要一个基本的解决方案,你可以使用 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");
}}

如果文件在

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-06
    • 1970-01-01
    • 1970-01-01
    • 2013-05-03
    • 2013-12-16
    • 2011-10-28
    • 1970-01-01
    相关资源
    最近更新 更多