ajax - Javascript Read Text File Offline - Stack Overflow

Currently I use this code to read a txt file with words and perform some operations. However, this part

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(",");

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(",");
Share Improve this question asked Feb 10, 2012 at 17:33 KerryKerry 1,1131 gold badge10 silver badges11 bronze badges 1
  • Why do you need to read the file? Why not just include the data in the page? – robertc Commented Feb 11, 2012 at 1:23
Add a ment  | 

3 Answers 3

Reset to default 3

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 puter 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' puter, 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 puter (e.g. file://c:/../test.html), you might be able to read files located on ONLY your puter by using relative paths.

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745307660a4621809.html

相关推荐

  • ajax - Javascript Read Text File Offline - Stack Overflow

    Currently I use this code to read a txt file with words and perform some operations. However, this part

    15小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信