0

Is it possible to access a specific file through javascript without the use of filechooser?

Let's say I want to read this file: C:\Users\...\aa.txt directly into a variable, without a filechooser.

If this isn't possible, what's the best way to do so?

BenM
  • 52,573
  • 26
  • 113
  • 168
Shanty
  • 3
  • 1
  • 4
  • 3
    No. That would bring serious security flaws. Best way to do it IS a file chooser... – BenM Mar 12 '13 at 09:53
  • What is the situation you are in? If you are in a website, not a locally hosted one, then no, this is not possible. Otherwise, sorry, you have to use a file chooser, it is the best and only way. – FabianCook Mar 12 '13 at 09:54
  • @BenM that's an answer if you ask me. 'No' may be too brief, but it's emphatic and important. Other people will ask this question and should see this as a firm response rather than a comment. – Barney Mar 12 '13 at 09:54
  • @Barney, thanks. I have expanded my comment a little, and added it as a comment. – BenM Mar 12 '13 at 09:56
  • Check this answer: http://stackoverflow.com/questions/1087246/can-javascript-access-a-filesystem it might prove helpful –  Mar 12 '13 at 09:56
  • Is it web page or XUL application or HTA? There are many ways to read file system with JavaScript, but no way from a browser (except IE with disabled security) – core1024 Mar 12 '13 at 10:01
  • @core1024,i just used 'IE with disabled security',because it's a local file. – Shanty Mar 12 '13 at 10:17

1 Answers1

1

This is not possible (thankfully). There would be serious security flaws in allowing a client-side script full access to a user's file system.

The only way to achieve this (on a website) is through a standard file input.

BenM
  • 52,573
  • 26
  • 113
  • 168
  • On a web page it is possible with drag and drop ;) http://www.html5rocks.com/en/tutorials/file/dndfiles/ – core1024 Mar 12 '13 at 10:05
  • Only for browsers that support that (~80%). – BenM Mar 12 '13 at 10:10
  • 1
    @core1024 that creates a local filesystem within the browser — it can't read from, write to or specify areas of the host's pre-existing filesystem. Even then, [Mozilla have made an executive decision not to implement this functionality for security reasons](https://hacks.mozilla.org/2012/07/why-no-filesystem-api-in-firefox/), and it's bleeding edge stuff, flakily implemented by a minority (the demo you linked to breaks on my Chrome, [the only desktop browser to currently support it](http://caniuse.com/#feat=filesystem)) — a [W3 draft](http://dev.w3.org/2009/dap/file-system/file-dir-sys.html). – Barney Mar 12 '13 at 10:38
  • I was talking about the `window.File` API (http://caniuse.com/#feat=fileapi), which *is* supported by all current browsers. Of course you can always fall back to `` if the browser does not support it ;) – core1024 Mar 12 '13 at 11:47