简体   繁体   English

Jsp中的使用条件

[英]How use conditions in Jsp <p:fileUpload

I need to check uploading file size,type and set uploaded file name to fileUpload value tag. 我需要检查上传文件的大小,类型,并将上传的文件名设置为fileUpload值标签。
I wrote simple functions for them in the uploadController and need to call them to check the file size and type. 我在uploadController中为它们编写了简单的函数,需要调用它们以检查文件的大小和类型。 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 或图像大小可以在浏览器端完成,在这里我找到了一个链接,例如单击此处 ,您也可以使用控制器,但是对于大文件和示例javascript代码来说效果更好

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 要么

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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