1

Is it possible to read xml files locally from an html page using javascript without using any local server?

Right now I am able to load xml using jquery.ajax function but it only loads xml files from server. What I need to do is to load xml from local file system. when i try to do it I get an error from chrome?

XMLHttpRequest cannot load file:///C:/Simplified.xml.

Cross origin requests are only supported for HTTP.

Nivas
  • 18,126
  • 4
  • 62
  • 76
J Roq
  • 171
  • 1
  • 8
  • As a general rule, javascript or any other browser language technology, cannot access local files. It would be a huge security hole. – spuriousdata Sep 09 '11 at 05:44
  • A question worth asking is - what is that security hole you're talking about? Your email client and many other applications access local files all the time. How is that not a huge security hole? – Anurag Sep 09 '11 at 05:58
  • serve the file, File API is pretty experimental – Drew Sep 16 '11 at 19:40

2 Answers2

0

You can run a server on your desktop. It is very very simple to set up an http server on almost any platform. They even come on a memorystick

That said,

Using this Google search I found Google Chrome --allow-file-access-from-files disabled for Chrome Beta 8 and possibly: Circumventing Chrome Access-control-allow-origin on the local file system?

Community
  • 1
  • 1
mplungjan
  • 169,008
  • 28
  • 173
  • 236
0

It's possible using some HTML5 API's. Checkout the FileReader API. Here's a working example. Tested in Chrome and Firefox. Does not work as of Safari 5.1.

// create a new FileReader object
var reader = new FileReader();
// setup an onload callback. This function
// will be called when the file has been 
// read/loaded in memory.
reader.onload = function(event) {
    console.log(reader.result);
};

// everything is set up. go read some files!
reader.readAsText(someFileInputField.files[0]);
Anurag
  • 140,337
  • 36
  • 221
  • 257