Restricting file upload size in JSF

别来无恙 提交于 2019-12-06 05:00:46

The web server can't abort a HTTP request halfway and then return a HTTP response. The entire HTTP request has to be consumed fully until the last bit before a HTTP response can ever be returned. That's the nature of HTTP and TCP/IP. There's nothing you can do against it with a server side programming language.

Note that the Tomahawk file upload size limit already takes care that the server's memory/disk space won't be polluted with the entire uploaded file whenever the size limit has been hit.

Your best bet is to validate the file length in JavaScript before the upload takes place. This is supported in browsers supporting HTML5 File API. The current versions of Firefox, Chrome, Safari, Opera and Android support it. IE9 doesn't support it yet, it'll be in the future IE10.

<t:inputFileUpload ... onchange="checkFileSize(this)" />

with something like this

function checkFileSize(inputFile) {
    var max = 500 * 1024 * 1024; // 500MB

    if (inputFile.files && inputFile.files[0].size > max) {
        alert("File too large."); // Do your thing to handle the error.
        inputFile.value = null; // Clears the field.
    }
}
Victor Jatobá

Try this:

<div>
    <p:fileUpload id="fileUpload" name="fileUpload" value="#{controller.file}" mode="simple" rendered="true"/>
    <input type="button" value="Try it" onclick="checkFileSize('fileUpload')" />

</div>

When user click in the button "Try it", checkFileSize() function is called and the fileUpload primefaces component is validated. If file size is greater than 500MB the file not is uploaded.

<script>
    // <![CDATA[
        function checkFileSize(name) {
            var max = 500 * 1024 * 1024; // 500MB
            var inputFile = document.getElementsByName(name)[0];
            var inputFiles = inputFile.files; 

            if (inputFiles.lenght > 0 && inputFiles[0].size > max) {
                alert("File too large."); // Do your thing to handle the error.
                inputFile.value = null; // Clears the field.
            }
        }
    // ]]>
</script>

The checkFileSize() logic is based on the BalusC answered above.

Versions tested:

primefaces 3.5

jsf 2.1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!