是否可以在非服务页面上加载带有JS/HTML5 FileReader的文件?

时间:2021-05-12 17:11:28

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)

我想用HTML5/JS创建一个简单的游戏,我不希望用户运行任何web服务器,也不希望用户必须连接到网站。(只是一个HTML页面)

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

但是看起来FileReader只能用于文件类型输入。

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 :

是否可能只有两个文件:索引。html和foo。并排并读foo。从指数三种。html格式如下:

// 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 ?

任何想法?

3 个解决方案

#1


8  

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

我相信这就是您的答案:如何使用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.

HTML5允许您加载本地存储在计算机上的文件,但是您不能选择它。用户必须选择要加载的文件。

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

想象一下,当开发人员(或者更好地说,黑客)能够访问每个人的本地数据时,会发生什么……

#2


1  

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.

这是一个老问题,我相信很多人都遇到同样的问题,因为一旦JS会对一个独立的应用程序(这是一个烦人的事情必须破解的方法,但不管怎样,我想越来越JS应用将客户机-服务器),然后它开始需要一些支持工具放在一起。

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.

以一种可维护的方式创建数据,然后将其传递给我正在使用的JavaScript的一种方法是编写一个简单的脚本,该脚本接受一组内容文件,并将内容解析为一个大型旧数据中的JSON。js文件。然后可以将其包含并执行与常规Javascript对象完全相同的操作。当然,也可以首先使用JSON存储数据,但如果应用程序有大量数据,这比简单的CSV要复杂得多。

#3


0  

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 :

我将JSON文件命名为.js: jsonfile。js,其中包含我的json数据作为string变量:

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

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

在我的索引。html,我包括它,在我的。js与代码:

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

然后我可以在mycode中把我的数据作为JSON对象。js是这样的:

var data = JSON.parse(jsondata);
alert(data.foo);

#1


8  

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

我相信这就是您的答案:如何使用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.

HTML5允许您加载本地存储在计算机上的文件,但是您不能选择它。用户必须选择要加载的文件。

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

想象一下,当开发人员(或者更好地说,黑客)能够访问每个人的本地数据时,会发生什么……

#2


1  

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.

这是一个老问题,我相信很多人都遇到同样的问题,因为一旦JS会对一个独立的应用程序(这是一个烦人的事情必须破解的方法,但不管怎样,我想越来越JS应用将客户机-服务器),然后它开始需要一些支持工具放在一起。

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.

以一种可维护的方式创建数据,然后将其传递给我正在使用的JavaScript的一种方法是编写一个简单的脚本,该脚本接受一组内容文件,并将内容解析为一个大型旧数据中的JSON。js文件。然后可以将其包含并执行与常规Javascript对象完全相同的操作。当然,也可以首先使用JSON存储数据,但如果应用程序有大量数据,这比简单的CSV要复杂得多。

#3


0  

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 :

我将JSON文件命名为.js: jsonfile。js,其中包含我的json数据作为string变量:

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

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

在我的索引。html,我包括它,在我的。js与代码:

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

然后我可以在mycode中把我的数据作为JSON对象。js是这样的:

var data = JSON.parse(jsondata);
alert(data.foo);