12

I can't find the way to detect if the browser supports the File API through the .support methon in jQuery.

Anyone knows it?

(Incidentally: a way to detect the size of a file in input[type=file] with IE?)

Naoise Golden
  • 8,793
  • 4
  • 49
  • 65

2 Answers2

11

It does not seem to be implementd in jQuery, but you could check yourself: http://jsfiddle.net/pimvdb/RCz3s/.

The files property of an <input type='file'> returns an empty FileList if it's implemented, and otherwise it is not defined (i.e. it is undefined).

var support = (function(undefined) {
    return $("<input type='file'>")    // create test element
                 .get(0)               // get native element
                 .files !== undefined; // check whether files property is not undefined
})();
pimvdb
  • 151,816
  • 78
  • 307
  • 352
  • More readable would be to check with `typeof` ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: `typeof $("").get(0).files != "undefined"` – jave.web Oct 31 '16 at 10:56
10

Another way to check is just by checking the presence of the File API types:

var FileApiSupported = !!('File' in window &&
                          'FileReader' in window &&
                          'FileList' in window &&
                          'Blob' in window);
glebm
  • 20,282
  • 8
  • 51
  • 67
  • I tested in firefox 22 that claims to have full support of this API and this solution didn't work. I don't know about others browsers. – Martin Jul 11 '13 at 05:04
  • 7
    `var FileApiSupported = !!(window.File && window.FileReader && window.FileList && window.Blob)` to get a boolean value – Niko Oct 11 '13 at 11:12