[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2905
  • Last Modified:

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

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
njgroup
Asked:
njgroup
  • 9
  • 5
  • 3
1 Solution
 
Dave BaldwinFixer of ProblemsCommented:
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
 
njgroupAuthor Commented:
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
 
Dave BaldwinFixer of ProblemsCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
njgroupAuthor Commented:
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
 
njgroupAuthor Commented:
so how to do that?
0
 
Dave BaldwinFixer of ProblemsCommented:
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
 
njgroupAuthor Commented:
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
 
Dave BaldwinFixer of ProblemsCommented:
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
 
njgroupAuthor Commented:
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
 
Dave BaldwinFixer of ProblemsCommented:
I don't know about vs.net2008.  You need to "Request Attention" and get some experts from the .NET areas.
0
 
Michel PlungjanIT ExpertCommented:
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
 
njgroupAuthor Commented:
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
 
Michel PlungjanIT ExpertCommented:
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
 
Michel PlungjanIT ExpertCommented:
http://www.cs.tut.fi/~jkorpela/forms/file.html

Note that the form submission can be done with ajax too
0
 
njgroupAuthor Commented:
how to do submission  in ajax?
0
 
njgroupAuthor Commented:
ok, I got it from that link, really good tutorial, thanks
0
 
njgroupAuthor Commented:
sorry, I forgot to set the question as answered for long time, I didnt notice it
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

  • 9
  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now