最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ajax - Javascript Read Text File Offline - Stack Overflow

programmeradmin0浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论