0

I'm looking to do some feature detection with JS to determine whether or not to show a typical 'input type="file"' element on a web page that will be accessed through desktops, tablets and phones.

Ideally I want to only show the input on devices/browsers that have full access to a file system. Full, meaning access to files (as on desktop computers), not just, say, the phone's camera files.

When I hit a file input on my Android phone on a web page I'm presented with a restricted list of things I can upload. In this case, I'd need the feature detection to return false, i.e. a full access filesystem is not present... I'm doubtful that this is possible. Worth saying, user agent sniffing is a last resort and something I'd wish to avoid.

Sam Hanley
  • 4,707
  • 7
  • 35
  • 63
Jack
  • 307
  • 4
  • 15
  • 1
    Strictly speaking, you can't really know *for sure* that a desktop browser is really providing "unlimited" file system access. – Pointy Jul 03 '13 at 21:42
  • 2
    That such a thing is possible seems exceedingly unlikely. Javascript in a web page is sandboxed up the wazoo, it can't access the filesystem **at all**. All it gets access to is single files that the user somehow chooses to hand to it, as well as a private sandbox using the (not widely supported) [FileSystem API](http://www.w3.org/TR/file-system-api/). – millimoose Jul 03 '13 at 21:43
  • @millimoose I think by "file system access" he's referring to what the file chooser dialog will show, like for `` – Pointy Jul 03 '13 at 21:50
  • 1
    @Pointy I know. My point is, your Javascript code can **never** know about anything besides the one file the user chooses in that dialog. (Or multiple if any of the newer HTML5 stuff allows for multiple file inputs, but still. Only files the user explicitly picks.) – millimoose Jul 03 '13 at 21:55
  • @pointy Correct. html was stripped from question. Now edited. – Jack Jul 03 '13 at 22:08
  • @millimoose consider a site that might want to allow the user to upload an XML file or an Excel spreadsheet. It wouldn't make much sense to offer that to an iPhone user. I think that's the sort of discrimination the OP is looking for. (Sadly, it's kind-of hard to do.) – Pointy Jul 03 '13 at 22:30
  • @Pointy Oh, I wasn't trying to say that there aren't valid use cases for this. Just that JS tries its damnedest, for security reasons, to not let you do this, use cases be damned. (In favour of more restricted ways to tackle said use cases.) – millimoose Jul 03 '13 at 22:35
  • @Pointy Yes exactly that. Only documents (doc, rtf etc) are allowed, so makes no sense to show upload button if on phone/tablet (at present at least. Give it a couple of years and who knows (which is why feature detection and not UA string sniffing is a more futureproof way of tackling the issue)). – Jack Jul 03 '13 at 22:43

0 Answers0