2

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>
DalusC
  • 391
  • 1
  • 12

1 Answers1

0

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

  • please refer https://stackoverflow.com/questions/7497404/get-file-size-before-uploading – Teja Venkat Lanka Dec 14 '18 at 13:58
  • But my issue is about how call my methods in **JSF** to check image file sizes, and image type before set the value in jsf-fileUpload. image type checking and image size checking methods already implemented in java. – DalusC Dec 17 '18 at 03:57
  • Either you need to write JavaScript using Ajax submit the request after validation or if you want to do it on server side you need to do it in one controller – Teja Venkat Lanka Dec 17 '18 at 04:25
  • If you want to use Java validation use one controller instead of two – Teja Venkat Lanka Dec 17 '18 at 04:30