Official Everybody Edits Forums

Do you think I could just leave this part blank and it'd be okay? We're just going to replace the whole thing with a header image anyway, right?

You are not logged in.

#1 2018-07-29 09:38:58, last edited by Anatoly (2018-07-29 09:39:38)

Anatoly
Guest

How do I read local text files?

Hello, Ive been searching for it, and my conclusion is, that XMLHttpRequest is bad. Anyone got ideas?

dmLoadContent();

function dmLoadContent() {
     var client = new XMLHttpRequest();
     client.open('GET', '/words.txt');
     client.onreadystatechange = function() {
          console.log(client.responseText);
     }
     client.send();
}

#2 2018-07-29 10:51:59

LukeM
Member
From: England
Joined: 2016-06-03
Posts: 3,009
Website

Re: How do I read local text files?

If you want to have a file selector popup then I think you can do that, but otherwise I'm pretty sure XMLHttpRequest is the only way //forums.everybodyedits.com/img/smilies/sad

Unless you want to use cookies I guess //forums.everybodyedits.com/img/smilies/tongue

(File selector thing: https://developer.mozilla.org/en-US/doc … input/file)

Offline

#3 2018-07-29 11:03:59

den3107
Member
From: Netherlands
Joined: 2015-04-24
Posts: 1,025

Re: How do I read local text files?

Depends. Are the files on the server or are you trying to access files on the client's computer?
In case of first:
Nah, XMLHttpRequset should be fine in combination with AJAX. As you currently have.
https://stackoverflow.com/a/13329895
In case of second:
For security reasons you can't just go and read files from the client on your own accord (for obvious reasons). If you still need client files, you can use something like this.
https://stackoverflow.com/a/26298948

Offline

#4 2018-07-29 11:07:02

LukeM
Member
From: England
Joined: 2016-06-03
Posts: 3,009
Website

Re: How do I read local text files?

den3107 wrote:

Depends. Are the files on the server or are you trying to access files on the client's computer?
In case of first:
Nah, XMLHttpRequset should be fine in combination with AJAX. As you currently have.
https://stackoverflow.com/a/13329895
In case of second:
For security reasons you can't just go and read files from the client on your own accord (for obvious reasons). If you still need client files, you can use something like this.
https://stackoverflow.com/a/26298948

I think atm he is just loading the page from an html file on his computer, in which case I would have thought that loading local files should be allowed, but maybe not I guess //forums.everybodyedits.com/img/smilies/tongue

Offline

#5 2018-07-29 12:28:34

Tomahawk
Forum Mod
From: UK
Joined: 2015-02-18
Posts: 2,847

Re: How do I read local text files?

You should put a [JS] tag in the title.

And then carry on googling. This subforum isn’t a search engine, and it’s a waste of everyone’s time to fetch you an answer to a generic question like that.


One bot to rule them all, one bot to find them. One bot to bring them all... and with this cliché blind them.

Offline

Wooted by: (2)

#6 2018-07-30 13:19:06, last edited by 1448 (2018-07-30 13:21:01)

1448
Formerly alkazam1448
From: Numberland
Joined: 2015-04-12
Posts: 683
Website

Re: How do I read local text files?

You could use the fetch API. It can load pretty much any type of file, and is recommended over XmlHttpRequests now. Also, if you want to load a local file using fetch, you can't; you need to host it on a server, because you can't fetch from file://

Offline

#7 2018-07-30 13:24:19

LukeM
Member
From: England
Joined: 2016-06-03
Posts: 3,009
Website

Re: How do I read local text files?

1448 wrote:

You could use the fetch API.

Oh wow... That looks useful

Its still experimental at the moment, so we probably shouldn't use it yet, but when its finished then I'm definitely going to be making use of this //forums.everybodyedits.com/img/smilies/big_smile

Not sure that it would solve the problems with loading local files though, as I'm guessing that it still has the cross origin restrictions are still in place

Offline

Wooted by:

#8 2018-07-30 13:50:47

Anatoly
Guest

Re: How do I read local text files?

1448 wrote:

you can't fetch from file://

But what can?

#9 2018-07-30 13:52:53

LukeM
Member
From: England
Joined: 2016-06-03
Posts: 3,009
Website

Re: How do I read local text files?

Anatoly wrote:
1448 wrote:

you can't fetch from file://

But what can?

I'm pretty sure only the file selector thing can, if you want to do something else then you'll need to find another way to do it //forums.everybodyedits.com/img/smilies/sad

What are you trying to use this for anyway? We might be able to help you to find alternatives if we have some background info.

Offline

Wooted by:

#10 2018-07-30 13:57:41

Anatoly
Guest

Re: How do I read local text files?

LukeM wrote:
Anatoly wrote:
1448 wrote:

you can't fetch from file://

But what can?

I'm pretty sure only the file selector thing can, if you want to do something else then you'll need to find another way to do it //forums.everybodyedits.com/img/smilies/sad

What are you trying to use this for anyway? We might be able to help you to find alternatives if we have some background info.

Saving data.

#11 2018-07-30 13:59:20

LukeM
Member
From: England
Joined: 2016-06-03
Posts: 3,009
Website

Re: How do I read local text files?

Anatoly wrote:
LukeM wrote:

Saving data.

We're going to need more info than that //forums.everybodyedits.com/img/smilies/tongue
What sort of data?

Offline

#12 2018-07-30 15:49:28

hummerz5
Member
From: wait I'm not a secret mod huh
Joined: 2015-08-10
Posts: 5,853

Re: How do I read local text files?

^ true.

This also depends on how much you want the user to be frolicking with it. Are they throwing it places? Either way, localStorage is pretty cool. 5MB max per domain is useful. Depending, again, on what you're storing.

Offline

#13 2018-07-30 15:53:49, last edited by LukeM (2018-07-30 16:03:23)

LukeM
Member
From: England
Joined: 2016-06-03
Posts: 3,009
Website

Re: How do I read local text files?

I spoke to him about this in game earlier, it seems like he wants to use this for loading a manual sort of configuration file, which localstorage isn't really for...

I've suggested that file inputs are probably the best way to do it, unless he wants to set up a website or something (when he can use text files)

Edit: Just thought, he might be able to get around the limitations by referencing it as a script assuming he doesn't want to write to it... (create a js file with a single const whateverData = { ... })

Edit 2: Wait... I'm sure I've managed to load local json files before....

Edit 3: This code seems to work...

var request = new XMLHttpRequest();
request.open("GET", "[filename].json", true);
request.responseType = "json";
request.onload = function() {
	//do something with request.response
};
request.send(null);

Why did you not just use this in the first place? Did it not work or something?
Not sure, but I think it should work with any type of file, not just JSON

Offline

#14 2018-07-30 15:56:23

hummerz5
Member
From: wait I'm not a secret mod huh
Joined: 2015-08-10
Posts: 5,853

Re: How do I read local text files?

^ That edit would probably be the easiest approach. A simple object with property/value pairs would be pretty logical for a config file.

Offline

#15 2018-07-30 16:13:48

Anatoly
Guest

Re: How do I read local text files?

Okay, there are many free websites.

Which one can save .txt/ .json files?

Also, I'm saving javascript arrays in the files.

#16 2018-07-30 16:18:42

LukeM
Member
From: England
Joined: 2016-06-03
Posts: 3,009
Website

Re: How do I read local text files?

Anatoly wrote:

Okay, there are many free websites.
Which one can save .txt/ .json files?
Also, I'm saving javascript arrays in the files.

I have a feeling that I might have misunderstood you before...
Do you also want to change the file, or is it just something that you load data from and occasionally manually change?

If you just want to load it, you can either use XMLHttpRequest or the javascript file thing I mentioned earlier, theres no need to use an external website.

If you also need to save the file, using an external website sounds like more effort than its worth... (By set up a website I meant run it locally on your computer)

Offline

#17 2018-07-30 16:27:03

drunkbnu
Formerly HG
Joined: 2017-08-16
Posts: 2,306

Re: How do I read local text files?

You can't edit local files unless you have a web server. You should really be using one for this kind of projects.

It's easy to configure Apache to mount a web server in your own computer, and even PHP (you're going to want this one to easily work with files) and MySQL (which you should be using to store your config).

You can even view your page more comfortingly without having to mess around with relative links when reading from file:/// (can't use / at the start of a link because it would lead to the root directory of the system, not the page).

Offline

#18 2018-07-30 16:55:40

LukeM
Member
From: England
Joined: 2016-06-03
Posts: 3,009
Website

Re: How do I read local text files?

Discussed this with AnatolyEE a bit more, and it seems like its a browser thing.
Firefox (which I'm using) allows you to load local files if you're testing a website locally, but some other browsers don't seem to allow this for some reason...

Offline

#19 2018-07-30 17:46:47

SirJosh3917
Formerly ninjasupeatsninja
From: USA
Joined: 2015-04-05
Posts: 2,095

Re: How do I read local text files?

use localStorage instead for saving & loading data

Offline

SirJosh39171532969207717021

Board footer

Powered by FluxBB

[ Started around 1732213527.455 - Generated in 0.461 seconds, 12 queries executed - Memory usage: 1.68 MiB (Peak: 1.91 MiB) ]