-1

I want to write a custom validator for advance file upload where i can check the size of file and throw a validation message if its greater than the maximum size. But, validator is never called. Can, anyone please help?

Bilal Dekar
  • 3,200
  • 4
  • 28
  • 53
user5268
  • 11
  • 6
  • 1
    look at this post (I mean do it like it is explained) [File Upload](http://stackoverflow.com/questions/39486157/get-uploaded-file-path-in-primefaces/39487964#39487964) and for the size look at the primefaces upload page [Primefaces Size](http://www.primefaces.org/showcase/ui/file/upload/single.xhtml) – Yagami Light Oct 24 '16 at 12:33
  • I want to show a validation error if the combined size of multiple files is greater then maximum size. – user5268 Oct 24 '16 at 12:52

2 Answers2

0

The file size that you upload with p:fileUplaod is unlimited by default, to set a limit in size you can use sizeLimit, if you uplaod a file which the size is greater then the limit an error message will showup. you can read more about it in the Primefaces web site Primefaces Upload File.

Yagami Light
  • 1,756
  • 4
  • 19
  • 39
Bilal Dekar
  • 3,200
  • 4
  • 28
  • 53
  • that sizeLimit is for a single file, I want to calculate the size of all the files and then see if that size is greater than the maximum size. – user5268 Oct 24 '16 at 12:49
0

First of all look at this post Upload File Step by Step to learn how to upload a file, and for the size limit you only need to check for all file size it will look like this :

Long size;
Long sizeLimite = 100000 ;
List<FileUploadEvent> e = new ArrayList<>();

and for each FileUploadEvent :

size = eventUpload.getFile().getSize() + size;
e.add(eventUpload);

and in the end :

if(size < sizeLimite ) {
 executUpload(e);
}
else{
FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage(null, new FacesMessage(" NO GOOD SIZE"));
}

In the example i gave you it's a loop you only have to copy it and make the first to check for the size, and the second to execute the upload method if the condition is true.

Community
  • 1
  • 1
Yagami Light
  • 1,756
  • 4
  • 19
  • 39
  • I called two method from the command button with the use of remote command.One is for file upload and one is for submitting data. I calculated the size and if size is greater i stored the Faces message. I want to show that message on xhtml without calling the other method for submitting data. – user5268 Oct 24 '16 at 13:28
  • i will update my answer – Yagami Light Oct 24 '16 at 13:29
  • is it what do you look for ?!? @user5268 – Yagami Light Oct 24 '16 at 13:49
  • In my form, file upload is not necessary, so i called second method from remote command. So, the problem is that Faces message is coming but just after that it calls second method. I want it to behave like a validator. – user5268 Oct 24 '16 at 14:05
  • you can avoid use the remote command i will update my answer to clear this point – Yagami Light Oct 24 '16 at 14:06
  • did you try oncomplete ?!? – Yagami Light Oct 24 '16 at 14:15
  • oncomplete of command button i called file upload listener and submit form methods.Before oncomplete of command button it should pass all the validators, so, i want to check this size functionality of file before oncomplete of command button. – user5268 Oct 25 '16 at 04:51
  • any evolution anything new ?!? – Yagami Light Oct 25 '16 at 07:56