简体   繁体   中英

How use conditions in Jsp <p:fileUpload

I need to check uploading file size,type and set uploaded file name to fileUpload value tag.
I wrote simple functions for them in the uploadController and need to call them to check the file size and type. Is there any recommended way to do this file validation ?

 <h:form enctype="multipart/form-data"> <p:growl id="messages" showDetail="true" /> <p:fileUpload // need set value after validating the file using BannerUpload.isPng() BannerUpload.checkMaxSize() value="#{BannerUpload.file}" mode="simple" skinSimple="true" /> <br/> <ui:fragment rendered="#{not empty BannerUpload.file}"> <img src="data:image/png;base64,#{BannerUpload.imageContentsAsBase64}" alt="" /> </ui:fragment> <br/> <p:commandButton action="#{BannerUpload.preview}" ajax="false" value="Preview" /> <br/> <p:commandButton value="Submit" ajax="false" action="#{BannerUpload.upload}" disabled="false" /> </h:form> 

For checking is it image ? or size of image can be done at browser side here i found a link click here for example and you can also do with controller but it not better for large file and for sample javascript code

function GetFileSize(fileid) {
try {
    var fileSize = 0;
    // for IE
    if(checkIE()) { //we could use this $.browser.msie but since it's deprecated, we'll use this function
        // before making an object of ActiveXObject, 
        // please make sure ActiveX is enabled in your IE browser
        var objFSO = new ActiveXObject("Scripting.FileSystemObject");
        var filePath = $("#" + fileid)[0].value;
        var objFile = objFSO.getFile(filePath);
        var fileSize = objFile.size; //size in b
        fileSize = fileSize / 1048576; //size in mb 
    }
    // for FF, Safari, Opeara and Others
    else {
        fileSize = $("#" + fileid)[0].files[0].size //size in b
        fileSize = fileSize / 1048576; //size in mb 
    }
    alert("Uploaded File Size is" + fileSize + "MB");
}
catch (e) {
    alert("Error is :" + e);
}

}

or

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM