Trouble with p:fileUpload allowTypes Restriction

UI Components for JSF
mspolari
Posts: 1
Joined: 13 May 2010, 17:13

13 May 2010, 17:36

Hi all,

I'm a primefaces n00b and I'm having an issue with the fileUpload component. I'm attempting to restrict the file type of the uploaded file to .pdf files exclusively, however the component is allowing the upload of any file I select. From my understanding of the documentation and online examples I should be able to filter the file selection by type using the allowTypes attribute of the fileUpload tag. My tag is as follows:

Code: Select all

<p:fileUpload fileUploadListener="#{pressMain.handleFileUpload}" 
 allowTypes="*.pdf" sizeLimit="4194304" auto="TRUE" 
 width="49" height="49" image="/resources/images/browse2.JPG"/>
I'm currently using Mojarra's JSF 2.0 Implementation, and running on Tomcat. The uploaded file is being handled and stored successfully so I don't believe my problem is in the backing code, but here it is anyway (theres not much to it really):

Code: Select all

public void handleFileUpload(FileUploadEvent event) 
{
	upload = event.getFile();
	if( upload != null )
	{
		FacesMessage msg = new FacesMessage("Succesful", upload.getFileName() + " is uploaded.");
		FacesContext.getCurrentInstance().addMessage(null, msg);
	}
		
}
My backing bean is session scoped and the upload variable is an instance of org.primefaces.model.UploadedFile.

Any advice you can give me about what I'm doing wrong would be useful. If you need any more info, just ask.

cagatay.civici
Prime
Posts: 18616
Joined: 05 Jan 2009, 00:21
Location: Cybertron
Contact:

18 May 2010, 17:07

Is it just pdf, how does it behave when you try other extensions like gifs, pngs?

wlove
Posts: 1
Joined: 11 Jun 2010, 17:11

11 Jun 2010, 17:19

I work with the original poster. We never did solve the allowType issue - PrimeFaces will allow any file upload type whenever the attribute was set to "*.pdf" However, we did the following in our backer:

Code: Select all

     org.primefaces.model.UploadedFile file = event.getFile();
     if (file.getFileName().toLowerCase().endsWith(".pdf")) {
          // do stuff
          uploadMessage = "File Uploaded Successfully";
     }
     else {
          uploadMessage = "Uploaded files must be in pdf format, please try again";
     }
The uploadMessage was a simple String variable, that was initialized to "", and displayed next to our upload icon.

prophet
Posts: 92
Joined: 17 Mar 2011, 19:59

22 Mar 2011, 18:22

I have the same problem! allowTypes doesn't work! I use apache and mojara jsf impl. what is a problem! Can you resolve it?

robert.m
Posts: 226
Joined: 07 Dec 2010, 22:52
Location: Salzburg/Austria

22 Mar 2011, 19:19

Using "*.pdf" as the allowTypes attribute may be wrong.

I use it like this: allowTypes="ogg,mp3" or allowTypes="ogg"
Last edited by robert.m on 23 Mar 2011, 12:16, edited 1 time in total.

prophet
Posts: 92
Joined: 17 Mar 2011, 19:59

22 Mar 2011, 21:36

No, I try to use allowTypes="*.xml;". It doesn,t work and allowTypes="*.jpg;", for example, doesn't work too! I try allowTypes="jpg" - no result. What's wrong?

robert.m
Posts: 226
Joined: 07 Dec 2010, 22:52
Location: Salzburg/Austria

23 Mar 2011, 01:19

which primefaces version do you use?

prophet
Posts: 92
Joined: 17 Mar 2011, 19:59

23 Mar 2011, 08:44

I use primefaces-2.2.1, thank you for reply!

robert.m
Posts: 226
Joined: 07 Dec 2010, 22:52
Location: Salzburg/Austria

23 Mar 2011, 12:17

Ist your fileUploadListener called?

prophet
Posts: 92
Joined: 17 Mar 2011, 19:59

23 Mar 2011, 15:58

yes called! like:

Code: Select all


     public void handleUpFile(FileUploadEvent event) {  
   
        FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");  
        FacesContext.getCurrentInstance().addMessage(null, msg);  

     } 

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 53 guests