Neelabh Singh
Neelabh Singh

Reputation: 2678

How to give url path of local file in json

I am trying to display the contents of a txt file myTutorials.txt, which is stored on my local disk, using json, But it does not display anything. I just want to know the correct formate of url "G:/WebTechnologies/myTutorials.txt";

The code of my web page is the following:

<!DOCTYPE html>
<html>
<body>

<div id="id01"></div>

<script>
var xmlhttp = new XMLHttpRequest();
var url = "G:/WebTechnologies/myTutorials.txt";

xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        var myArr = JSON.parse(xmlhttp.responseText);
        myFunction(myArr);
    }
}
xmlhttp.open("GET", url, true);
xmlhttp.send();

function myFunction(arr) {
    var out = "";
    var i;
    for(i = 0; i < arr.length; i++) {
        out += '<a href="' + arr[i].url + '">' + 
        arr[i].display + '</a><br>';
    }
    document.getElementById("id01").innerHTML = out;
}
</script>

</body>
</html>

Upvotes: 1

Views: 17735

Answers (2)

elrado
elrado

Reputation: 5282

you have to prepend file://localhost

EXAMPLE: file://localhost/G:/WebTechnologies/myTutorials.txt

Because of security reasons you will probably have to do loading text file through Ajax

Upvotes: 1

Jos de Jong
Jos de Jong

Reputation: 6819

Reading arbitrary files from the disk is not allowed for security reasons.

In modern browsers you can use the HTML5 FileReader API to read files from the disk.

Upvotes: 1

Related Questions