read txt file via client javascript

后端 未结 4 2122
小蘑菇
小蘑菇 2020-12-15 11:23

I\'m new to javascript and trying to open a txt file into var and then inject it to html div... I tried to use fopen but I didn\'t succeed.



        
相关标签:
4条回答
  • 2020-12-15 12:06

    For security reasons, Javascript is made so you can not do this. However, a person has made a workaround which may work and posted it here.

    Ok, I realize, it only works for files that are publicly accessbile on the server, which I believe is not what you want to do. Still, if you do find a way, it will be a hack like this, but it could also be fixed to not work at any time.

    0 讨论(0)
  • 2020-12-15 12:07

    abandoned 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

    0 讨论(0)
  • 2020-12-15 12:07

    Although it says xml request this works perfectly fine for txt files too (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;
    
    0 讨论(0)
  • 2020-12-15 12:14

    JavaScript has none of the functions 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 the client machine.

    0 讨论(0)
提交回复
热议问题