Read txt file through client javascript

I am new to javascript and trying to open a txt file in var and then add it to the html div ... I tried to use fopen but I failed.

<script type="text/javascript"> file = fopen(getScriptPath("info.txt"), 0); file_length = flength(file); var content = fread(file,file_length); var div = document.getElementById("myDiv"); //alert(div); div.innerHTML = ""; div.innerHTML = content; </script> 
+6
source share
4 answers

left question:

 if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.open("GET","YOUR_FILE.txt",false); xmlhttp.send(); xmlDoc=xmlhttp.responseText; 

by Freek8

+2
source

Although he says the xml request works fine for txt files (server and client side).

 if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.open("GET","YOUR_FILE.txt",false); xmlhttp.send(); xmlDoc=xmlhttp.responseText; 
+10
source

JavaScript doesn't have any of the features you are trying to use.

To read files on the server in JavaScript, you can use XMLHttpRequest .

There is no easy way to read files on a client machine.

+8
source

For security reasons, Javascript is designed in such a way that you cannot do this. However, the man made a workaround that could work and posted it here .

Well, I understand, it only works for files that are publicly available on the server, and I believe that this is not what you want to do. However, if you find a way, it will be such a hack, but it can also be fixed so as not to work at any time.

+5
source

Source: https://habr.com/ru/post/901512/


All Articles