【问题标题】:Display file size with two decimal points in PrimeFaces file upload componentPrimeFaces文件上传组件显示文件大小带两位小数
【发布时间】:2022-12-09 01:19:30
【问题描述】:

我们有文件上传功能。最大文件上传大小为 3 MB,我们正在组件级别验证文件大小。

p:fileUpload组件中使用sizeLimit="3000000"

我们正在显示如下错误消息

允许上传的最大文件大小为 3 MB。:XXX.pdf 7.5 MB

但我的要求是显示带有两位小数的文件大小,如下所示

允许上传的最大文件大小为 3 MB。:XXX.pdf 7.53 MB

有可能吗?

我尝试使用 messageTemplate 和 jQuery 但无法做到。

【问题讨论】:

  • “允许的最大文件大小”消息从何而来?

标签: jsf file-upload primefaces filesize upload-max-filesize


【解决方案1】:

一种解决方案是使用原型覆盖 PrimeFaces js 中的尺寸计算方法。

然后代码应该如下所示:

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
    xmlns:f="http://xmlns.jcp.org/jsf/core"
    xmlns:p="http://primefaces.org/ui"
    xmlns:c="http://java.sun.com/jstl/core">
<h:head>
</h:head>
<h:body>

    <script>
        PrimeFaces.widget.FileUpload.prototype.formatSize = function(a) {
            if (a === undefined) {
                return '';
            }
            if (a === 0) {
                return 'N/A';
            }
            var b = parseInt(Math.floor(Math.log(a) / Math.log(1024)));
            if (b === 0) {
                return a + ' ' + this.sizes[b];
            } else {
                return (a / Math.pow(1024, b)).toFixed(2) + ' ' + this.sizes[b];
            }
        };
    </script>


    <h:form id="frmClientDetails">
        <p:fileUpload value="#{fileUploadView.file}" mode="advanced"
            sizeLimit="100" invalidSizeMessage="Your error message" />
    </h:form>

</h:body>
</html>

这样做你在错误消息和详细信息中都设置了两位小数(我在 PF 6.2 js 中添加的唯一区别)。

【讨论】:

    猜你喜欢
    • 2014-09-11
    • 2013-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-04
    • 1970-01-01
    • 2010-09-21
    相关资源
    最近更新 更多