wood burning stoves 2.0*
The moose likes JSF and the fly likes File upload filter for displaying files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "File upload filter for displaying files" Watch "File upload filter for displaying files" New topic
Author

File upload filter for displaying files

Steve Renard
Ranch Hand

Joined: Apr 27, 2007
Posts: 66
I'm using JSF/Tomahawk library for my application.
For a file upload functionality, I have used t:inputFileUpload tag.
It works fine and am able to upload & download back the file.
But, how do I filter files in the file selection dialog to display only *.doc files?
Any help is appreciated.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
The HTML input type="file" element has an 'accept' attribute which is specified by the W3 HTML specification as optional. You can define in there the content types (mime types) which are to be displayed in the browse dialog only. Unfortunately a lot of webbrowsers doesn't support it. As far now only Opera, which very strictly follows the W3 HTML spec, supports it.

To fullfill this requirement, you need to validate it at the server side at any way. You can use ServletContext#getMimeType() for it to validate the file extension against the mime mapping configuration of the appserver in question. Alternatively you can use URLConnection#guessContentTypeFromName() to validate the file extension against the file type configuration of the underlying operating system.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: File upload filter for displaying files