reading server file with javascript

后端 未结 2 1792
夕颜
夕颜 2020-12-05 12:19

I have a html page using javascript that gives the user the option to read and use his own text files from his PC. But I want to have an example file on the server that the

2条回答
  •  遥遥无期
    2020-12-05 13:04

    The usual way to retrieve a text file (or any other server side resource) is to use AJAX. Here is an example of how you could alert the contents of a text file:

    var xhr;
    if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    xhr.onreadystatechange = function(){alert(xhr.responseText);};
    xhr.open("GET","kgr.bss"); //assuming kgr.bss is plaintext
    xhr.send();
    

    The problem with your ultimate goal however is that it has traditionally not been possible to use javascript to access the client file system. However, the new HTML5 file API is changing this. You can read up on it here.

提交回复
热议问题