Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the contents of a *user-selected* *local* file in javascript without any posts, gets, submits, etc

I'm using jQuery if that's any benefit; and I would like to have:

  1. The user selects a file on the local filesystem

  2. my javascript gets the contents of said file without going "across the wire".

  3. I am checking the browser type and am prepared to use completely different code for each browser. IE and FF are the two that I am currently writing for.

  4. I am Not using php, just straight javascript on a form.

  5. I do not want any gets, posts, submits, ajax, or any other network activity until the contents of the file have been acquired, validated, and manipulated (all of which my script will do; this question is just for getting the guts of the file).

  6. I do not necessarily need to get the path. I don't particularly want to get the path. The generally accepted notion of "secure" having the user pick the file and my script just gets the contents. That's fine, but if I get the path I won't complain.

I've googled this a lot and haven't gotten any solution that simply works.


I've looked at Mozilla XPCOM, but the file picker nsIFilePicker requires privilege escalation, which I just don't want to deal with.


I've tried using a regular

<input type="file" onclick="fileCheck(this); return false" . . . />

with

function fileCheck ()
{
  var path = obj.files[0].mozFullPath;
}

But even though you can see mozFullPath in Firebug, it does not show up for Javascript (ostensibly for security reasons).

Any ideas?

like image 556
kovacsbv Avatar asked Jan 03 '12 17:01

kovacsbv


2 Answers

You can; have a look at HTML5 Rocks - Reading local files in JavaScript.

like image 191
Jakub Roztocil Avatar answered Oct 21 '22 08:10

Jakub Roztocil


You can't, for blindingly obvious security reasons.

like image 36
Lightness Races in Orbit Avatar answered Oct 21 '22 08:10

Lightness Races in Orbit