0

Is this possible to programmatically open any file on disk using HTML5 File API, for example adding at first those paremeters?

open -a Chromium --args --allow-file-access-from-files --disable-web-security
bartek
  • 2,921
  • 5
  • 26
  • 30

1 Answers1

0

First you have to open Chrome using the flag --allow-file-access-from-files Make sure all your chrome windows are closed before opening it with the flag.

Then with javascript you have to use the XMLHttpRequest object. Something like this:

xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET","file.txt",false);
xmlhttp.send();
var result = xmlhttp.responseText;

In result you will have the content of the file.

If it's an xml you can automatically parse it using

var xmlDom = xmlhttp.responseXML;

The file path has to be relative to the html file you are opening.

Rubén
  • 524
  • 5
  • 22
  • cool. And what about reading files reqursively? XMLHTTPRequest cannot help here. – bartek Apr 22 '13 at 15:33
  • @bartek Oh you can't do that my friend it was decided for security reasons that the only binary files or random files have to be chosen with a filechooser. Either way with just a tiny javascript code anyone could read all the files of people connecting to a site, or even execute the files and stuff. That's also why the allow-file-acces-from-files is restricted by default and you should not navigate the internets with that flag on. However you can code a Java applet and do that with Java :) – Rubén Apr 22 '13 at 15:37
  • Maybe I could drag'n'drop folder, get names-tree of files in that way, and later read each file using `XMLHTTPRequest`. – bartek Apr 22 '13 at 15:39
  • I want to use `allow-file-acces-from-files` for my offline tool, it will never show up online for wider audience. Therefore all hacks for reading tree structure of files are acceptable for me. – bartek Apr 22 '13 at 15:41
  • Then use a Java applet, the javascript api for reading files is really very limited and restricted. – Rubén Apr 22 '13 at 15:41
  • I want to read files tree structure from any position on disk and then upload it to webservice. Can't be done with folder drag'n'drop and xmlhttprequest later? – bartek Apr 22 '13 at 15:44
  • Nope, that won't work. XMLHttpRequest is for reading "readable" files. You can't read binary files with that and also you can only read in the relative path of the folders inside the same folder of the html file. – Rubén Apr 22 '13 at 15:45
  • ok. And what about chrome extensions, are you familiar with it, how easy it would be with that approach? – bartek Apr 22 '13 at 15:47
  • well, I see it's possible to read binary data: http://hublog.hubmed.org/archives/001941.html – bartek Apr 22 '13 at 15:49
  • @bartek I found this http://www.w3.org/TR/html5/editing.html#dnd for the drag and drop – Rubén Apr 22 '13 at 16:04