Can you read a txt file in javascript with the file path in the code? Not by selecting the file from the open file window.
-
Load it with use of XHR. – Nonemoticoner Apr 28 '16 at 21:47
-
1Yes you can, it's called ajax, or more specifically XMLHttpRequest, good luck with that – adeneo Apr 28 '16 at 21:47
-
http://stackoverflow.com/questions/14446447/javascript-read-local-text-file – Nishant Jani Apr 28 '16 at 21:47
-
No you can't. Without user interaction you cannot access any file on the local system on a typical browser. – Derek 朕會功夫 Apr 28 '16 at 21:48
-
2The best way to find out the answer to these kinds of questions is to try. If you can't figure that out, search the internet. If you can't find it on the internet, ask a question on SO about the specific problems you're having. – Heretic Monkey Apr 28 '16 at 21:48
-
http://stackoverflow.com/questions/14446447/javascript-read-local-text-file#14446538 – Danny Fardy Jhonston Bermúdez Apr 28 '16 at 22:01
-
@people No-one said it's local (nor on the server) so... please Alb, clarify your thoughts and show what you tried and researched about that topic. Don't ask others to use Google instead of you ;) – Roko C. Buljan Apr 28 '16 at 22:03
-
@RokoC.Buljan its a local file from my pc – Alb Apr 28 '16 at 22:10
2 Answers
If the textfile is on a local or private PC
As you may understand if you could read a textfile that is stored on a user's filesystem everyone would be able to steal private data, so in short NO you can't.
If your textfile is on your server
ajax
function ajax(a,b,c){ // Url, Callback, just a placeholder
c=new XMLHttpRequest;
c.open('GET',a);
c.onload=b;
c.send()
}
how to use:
ajax('http://YOURSERVER/yourtextfile.txt',function(){
alert(this.response);
});
If your textfile is on a different server
When you tray to access other servers with ajax you need to be allowed to access that file. some sites allow it by returning
Access-Control-Allow-Origin: *
in the response heaeders.so the above ajax function would work properly.
Then there are other ways to get data from your or other servers that i prefer over ajax:
websockets & SSE.. but those need a specific interface like php or nodejs.
Another option is if the file is on your private pc and you just want to send some data that is stored in a specific textfile everytime you update:
Just install nodejs or a free PHP server and create some sort of cron job to check a specific folder everyonce a while. When the file is updated just send it to your online server. Again the online host needs at least PHP ASP or some sort of serverside scripting.
you can use XHR (XML Http Request). Below is an example for reading a '.txt' file. You can have this in an html file and will need to run a simple server using NodeJS, Python Simple Server or any other server that you are comfortable with.
1 If you have python:
python -m SimpleHTTPServer 8000
2 Example (readtxt.html):
<script type="text/javascript">
var request = new XMLHttpRequest();
request.open('GET', 'filename.txt', false); // `false` => synchronous request
request.send(null);
if (request.status === 200) {
console.log(request.responseText);
}
</script>
3 Open: http://localhost:8000/readtxt.html
4 Check the browser console for content read from the txt file.
You can read this for more detailed information,

- 2,360
- 1
- 27
- 28