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

核能气质少年 提交于 2019-11-29 04:01:17
DRAX

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...

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.

I have a cheat code for this case.

I named my JSON file as a .js : jsonfile.js , which contains my json data as a string variable :

var jsondata = '{ "foo" : "bar" }';

In my index.html, I include it, before my .js with the code :

<html> 
<head> 
<script type="text/javascript" src="mydata/jsonfile.js"></script>
<script type="text/javascript" src="js/mycode.js"></script>

Then I can get my data as JSON object in mycode.js like this :

var data = JSON.parse(jsondata);
alert(data.foo);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!