theodore Posted January 9, 2008 Share Posted January 9, 2008 I am doing a coursework, and I have chosen HTML with JavaScript to do it.I have to read a text file and I have chosen AJAX to do it: function start(DocFile){ DocFile="file://"+DocFile; DocObject = getXmlHttpObject(getFileContent); DocObject.open("GET", DocFile , true); DocObject.send(null); }function getFileContent(){ if (DocObject.readyState==4 || DocObject.readyState=="complete"){ cDoc=DocObject.responseText; ....{main function} }} Originally, everything was alright. However, I have found that there is a big problem!!1. When I open the main webpage stored in a network drive, the webpage can only read text files from the drive but cannot read local drives files....2. When I upload the web page in a web hosting site, I cannot read any local files!!!Is this problem related to permission issue????I feel very sad about this problem. Please help me!!Do I have any other alternatives to read file???? (I don't want to include JAVA or swf or Applet)Thank you very very much!!! Link to comment Share on other sites More sharing options...
jesh Posted January 9, 2008 Share Posted January 9, 2008 Try using relative paths rather than the "file" scheme. If you have a directory called "website" and the page that is using the ajax (index.html) and the text file that ajax is trying to open (text.txt) are both in that directory, then you can use the following: DocFile="text.txt"; Also keep in mind that unless you are using a web server to run this test locally, you're most likely not going to be getting any response codes from the ajax request (the readyState property may contain values that you don't expect). Link to comment Share on other sites More sharing options...
justsomeguy Posted January 9, 2008 Share Posted January 9, 2008 Is this problem related to permission issue????Yes - an AJAX request is only allowed to go to the same domain as the page making the request. So pages on domain.com can only send AJAX requests to domain.com, nowhere else. Using relative links will help if the files you want to request are on the web server hosting the page. It will only work with local files if you open the file locally (by double-clicking on it). I've actually never even tried to run an AJAX application without a web server, so I'm not sure if that will even work in the first place. Link to comment Share on other sites More sharing options...
theodore Posted January 10, 2008 Author Share Posted January 10, 2008 Thank you!Is there any other methods (JavaScript) which can read files from local drives, with running a online webpage? Link to comment Share on other sites More sharing options...
Synook Posted January 10, 2008 Share Posted January 10, 2008 No - again, for security issues. There is an ActiveX control (can't remember the name) that allows limited access but of course it would only work in Internet Explorer. Link to comment Share on other sites More sharing options...
justsomeguy Posted January 10, 2008 Share Posted January 10, 2008 Yeah, that would be bad if you go to a website and it starts reading your files. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.