Solved

how to read/write text file in javascript or jquery?

Posted on 2010-09-20
19
2,364 Views
Last Modified: 2013-11-26
hi,
how to read/write text file in javascript or jquery?

I found a code snippet, but it give me access is dined.

 
function read_txt_file(path) {
	        var allText = "";
	        var txtFile = new XMLHttpRequest();
	        txtFile.open("GET", path, true);
	        txtFile.onreadystatechange = function() {
	            if (txtFile.readyState === 4) {  // Makes sure the document is ready to parse.
	                if (txtFile.status === 200) {  // Makes sure it's found the file.
	                    allText = txtFile.responseText;
	                    //lines = txtFile.responseText.split("\n"); // Will separate each line into an array
	                }
	                else {
	                    allText = "NoText";
	                }
	            }
	        }
	        txtFile.send(null);
            return allText;
	    }

call the function:
            var path = "file://D:/dns.txt";
            var text = read_txt_file(path);
            alert(text);

Open in new window

0
Comment
Question by:njgroup
  • 9
  • 5
  • 3
19 Comments
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 33719520
Javascript is normally denied access to the client computer.  There is supposedly a way to create 'signed' scripts but the user would still have to accept them for them to access local files.
0
 

Author Comment

by:njgroup
ID: 33719660
no, I do not want to access client pc, but some files in same of website directory

because I was thinking it would be better to access these files using javascript directly without ajax (because its time consuming)
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 33720348
Using "file://D:/dns.txt" declares a local file on the client computer.  If you use "dns.txt", it will look on the web server.
0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 

Author Comment

by:njgroup
ID: 33721564
I didn't understand very well,

I have dns.txt in my computer now, my target is that when I am gonna publish the website, I am going to have some text file in the root directory, so I want from that javascript function to open and get text from text file in root directory
0
 

Author Comment

by:njgroup
ID: 33721565
so how to do that?
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 33721582
javascript and web pages in general can only open files that are in the web root directory.  They don't have permission to read anywhere else.
0
 

Author Comment

by:njgroup
ID: 33721771
ok, great, that what I need, to open text files in root directory

how?

for example, for img tag, src="images/my_image.jpg"
how about text file in javescript? do I need to make it as: http://www.my_web_site.com/files/my_file.txt
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 33722841
You should be able to make it a 'relative' link like "files/my_file.txt".  But I can't think of any way to load it in javascript without using AJAX.  If you are using PHP or ASP you can 'include' it where you want it in the page.
0
 

Author Comment

by:njgroup
ID: 33723227
how to include it in the page?

I was using ajaxpro which is not compatible with vs.net2008,

so I need some other way, how I can do that?
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 33726723
I don't know about vs.net2008.  You need to "Request Attention" and get some experts from the .NET areas.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 33732049
1. put the file on the server
2. read it with ajax
3. change it on the client
4. send it to a server process to save it.
javascript cannot write a file on the client nor on the server without some process doing the actual writing.

http://www.google.com/search?q=.net+ajax+file+upload
0
 

Author Comment

by:njgroup
ID: 33739570
thanks, but I need only:

read text file with ajax from server, update it in javascript, then save it back in server.

I do not need to upload files!
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 33741162
Yes you do. That is what saving a file does, it uploads it and to save it you need code on the server that handles uploads
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
ID: 33741428
http://www.cs.tut.fi/~jkorpela/forms/file.html

Note that the form submission can be done with ajax too
0
 

Author Comment

by:njgroup
ID: 33742365
how to do submission  in ajax?
0
 

Author Comment

by:njgroup
ID: 33742373
ok, I got it from that link, really good tutorial, thanks
0
 

Author Closing Comment

by:njgroup
ID: 33810638
sorry, I forgot to set the question as answered for long time, I didnt notice it
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question