Question

Currently I use this code to read a txt file with words and perform some operations. However, this particular code requires the html to be deployed on a server. Is there any workaround where I can replace this code with something else to read the file without the need of a server?

var xhr = new XMLHttpRequest(); 
xhr.open( "GET", "dictionary.txt", false );
xhr.send( null );
var words= xhr.responseText.split(",");
Was it helpful?

Solution

You can hide an iframe on the page, with its src='dictionary.txt',

and read or manipulate the iframe's local content when it's onload event fires.

OTHER TIPS

It is NOT possible to call Ajax outside your server domain (except you use scriptagproxy, that too requires you to have some server side configuration). So, in short, you CANNOT read files on your local computer using Ajax calls.

You might like this article.

The file selection can be made either by input or drag-and-drop (not otherwise). Please see: this

You cannot read files from the clients' computer, so the text file you are reading must be on the same server as your javascript.

However, if you are loading the HTML file from your computer (e.g. file://c:/../test.html), you might be able to read files located on ONLY your computer by using relative paths.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top