Ajay
Ajay

Reputation: 7428

Reading a file using javascript

How do I read contents from a server side file using javascript?

Upvotes: 0

Views: 1106

Answers (5)

rahul
rahul

Reputation: 187040

This is not possible using plain javascript. Javascript runs in the client browser and you cannot access a file in server. You can use AJAX to do this.

Upvotes: 2

user1228
user1228

Reputation:

You have to serve the file via a HTTP request (i.e., the file is available as a URL like www.conphloso.com/somefile.txt), which you can grab via an ajax request in the background.

Upvotes: 2

Justin Dearing
Justin Dearing

Reputation: 14938

The quick answer is "you can't".

If you make the server side file accessible through your web server, you can use an xmlhttprequest, a.k.a ajax, to retrieve it.

Upvotes: 1

Warren Young
Warren Young

Reputation: 42343

Ask the web server for it with Ajax. In jQuery speak, for instance:

jQuery.get('path/to/file/on/server.txt', null, function(data, status) {
    // your file contents are in 'data'
});

Upvotes: 10

veggerby
veggerby

Reputation: 9020

using Ajax (XmlHttpRequest) e.g. using jQuery:

jQuery.get( url, [data], [callback], [type] )

Upvotes: 2

Related Questions