Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to load a file with JS/HTML5 FileReader on non served page?

I want to create a simple game in HTML5/JS and I don't want the user to run any webserver or to have to connect to a website. (just an HTML page)

But it looks like FileReader can only be used on files type inputs.

Is it possible to have only two files : index.html and foo.txt side by side and to read foo.txt from index.html with something like :

// No input needed, I know waht I want to read
var my_file = new File("foo.txt");
var reader = new FileReader();
alert( reader.readAstext( my_file, "UTF-8" ) );

Any idea ?

like image 629
MARTIN Damien Avatar asked Feb 27 '12 23:02

MARTIN Damien


2 Answers

I believe that this is your answer: How to open a local disk file with Javascript?

In short, you are looking something like this:

<input type="file" id="files" name="file" />

HTML5 allows you to load files which are stored locally on computer, but you cannot select it. User must select file which he/she wants to be loaded.

Just imagine what would happen when developers (or better spoken, hackers) would have access to everyones local data...

like image 83
DRAX Avatar answered Oct 11 '22 14:10

DRAX


This is an old question and I'm sure that plenty of people have run into the same problems since but once JS gets towards being a standalone application ( and this is an annoying thing to have to hack one's way around but I guess increasingly JS apps will be client-server anyways ) then it starts to be necessary to put together some supporting tools anyway.

One way to create data in a maintainable way and then pass it to JavaScript that I am using is to write a simple script that takes a set of content files and parses the content into JSON in a big old data.js file. This can then be included and behave exactly the same as regular Javascript objects. One could also use JSON to store the data in the first place, of course, but that is a lot more verbose than something like simple CSV if you have a lot of data for your application.

like image 35
glenatron Avatar answered Oct 11 '22 13:10

glenatron