5

Im wondering what the best way to check if the input type="field" field is available to the user?

At least iPhones and iPads does not allow the user to upload files, but rather than checking the user agent with backend code i'd like to hear what you think is the most minimalist solution?

The purpose is to check if file upoads is available, if not, set display:none; for the whole div containing the form.

Can this be done with only css? jQuery?

I'd prefer a solution which not explicitely does not check if(iOS) but rather does a more generic check.

Thank you in advance!

jonas
  • 620
  • 1
  • 6
  • 14
  • If you don't give opera mini access to disk, it will also convert the file upload input fields to text type automatically. I would like to know the solution too. – Prasanth Aug 07 '12 at 10:28

1 Answers1

12

The most elegant way to handle this as of today is probably using Modernizr which in its latest version allows you to test for file input support with the following JS statement:

if(Modernizr.fileinput) {
    //file input available!
} else {
    //No file input :(
}

Alternatively if you don't wish to include Modernizr in your project there is a simple a way to test for it which only involves 3 lines of code:

var elem = document.createElement('input');
elem.type = 'file';
return !elem.disabled;

As described in this SO answer.

Community
  • 1
  • 1
Mahn
  • 16,261
  • 16
  • 62
  • 78
  • 1
    Great, but feels a bit heavy to load a full library for one function. – jonas Aug 07 '12 at 10:27
  • 1
    [found in Modernizr documentation.](http://miketaylr.com/code/input-type-attr.html) – Prasanth Aug 07 '12 at 10:32
  • 2
    There seems to be a flaw in this in iOS 5+, due to how iOS handles the file input -- the Modernizr test passes, because you can technically upload *images*, though not files in general (no file system access). As such, file upload isn't disabled entirely. – Shauna Mar 10 '14 at 18:46
  • 1
    You can verify with the test case linked [here](https://github.com/Modernizr/Modernizr/issues/497#issuecomment-3933123) – Shauna Mar 10 '14 at 18:54
  • I checked this morning for file input test on modernizr, but i think that on the actual 2.8.3 release there is no longer Modernizr.fileinput test – pastorello Mar 12 '15 at 11:49