문제

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(",");
도움이 되었습니까?

해결책

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.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top