Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to check if user can upload files?

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!

like image 593
jonas Avatar asked Aug 07 '12 10:08

jonas


1 Answers

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.

like image 115
Mahn Avatar answered Oct 04 '22 00:10

Mahn