• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

File filtering in file chooser

 
Bartender
Posts: 11445
18
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My application needs to accept only csv files
I set

However when I run the code, I still see an option called "All Files"
Is this a browser thing or is there any other parameter I need to use to explicitly force only csv files?
Screen-Shot-2016-10-05-at-4.55.57-PM.png
[Thumbnail for Screen-Shot-2016-10-05-at-4.55.57-PM.png]
 
Saloon Keeper
Posts: 5770
146
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The accept parameter is more a hint to the browser which files to show to the user, rather than a way to prevent the upload of other file types. Also, it's just a hint, so browsers are free to act on it as they see fit (and different browsers do indeed handle it differently). You need to validate on the server anyway in order to ensure that users didn't fiddle with the extension.

Check out https://jsfiddle.net/jhfrench/cukjxnp6/ for a JavaScript solution that may do what you want.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tim!

In Swing, one can absolutely control the allowed file types. I was hoping for something similar.
I do validate. However it is after the user has chosen the file. Unfortunately that means more mouse clicks which is bad usability and something which I was hoping to avoid.
Looks like I will have to live with it, since I have no control. Sigh.
 
Sheriff
Posts: 6377
1124
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Maneesh Godbole wrote:Unfortunately that means more mouse clicks.


Doesn't "comma-separated values" show up as the default option?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes it does. But that does not stop the user from selecting all values and play mischief.
 
Saloon Keeper
Posts: 10506
224
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What kind of mischief? You will have to perform validation regardless of whether the browser allows the user to only pick files with a specific extension.

Look at it this way: The filter is a convenience for the user, so it's easier for them to find the correct file. It's not a convenience for the developer, so they have to perform less checking.
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Used to be that the File upload Input box was seriously limited. It used to be very frustrating to deal with it.

In HTML5, there was a major improvement to the File chooser, and there is a Javascript API that lets you do advanced querying of file attributes and filtering. https://www.html5rocks.com/en/tutorials/file/dndfiles/ However, this means that you will have to upload the files using Javascript. It's more complicated to implement than dropping a control on an HTML page. I believe I have seen someone create an angular directive that allows you to drop a HTML tag in an HTML page that supports more features than the standard file upload (and if you are not using angular and didn't understand a word of the preceding line, don't worry about it :lol:)

Obviously, this won't work if your user has disabled Javascript. Most websites will provide a feature rich uploader that works only when Javascript is enabled, and fall back to the standard uploader when Javascript is disabled.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For those who did not really understand the usability perspective, this is a JFileChooser set to accept only csv files.
Notice the only option available is csv. Of course the user can still navigate the directory hierarchy.
Screen-Shot-2016-10-15-at-7.43.53-PM.png
[Thumbnail for Screen-Shot-2016-10-15-at-7.43.53-PM.png]
 
The government thinks you are too stupid to make your own lightbulb choices. But this tiny ad thinks you are smart:
create, convert, edit or print DOC and DOCX in Java
https://products.aspose.com/words/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!