• Post Reply Bookmark Topic Watch Topic
  • New Topic

File filtering in file chooser  RSS feed

 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • 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]
 
Tim Moores
Saloon Keeper
Posts: 3654
78
  • 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 Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • 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.
 
Devaka Cooray
Marshal
Posts: 5388
609
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • 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 Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • 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.
 
Stephan van Hulst
Saloon Keeper
Posts: 7376
130
  • 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.
 
Jayesh A Lalwani
Rancher
Posts: 2762
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 Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • 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]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!