1

I'm using ajax call to load data to my application. its working fine for paths like this

../../DataSource/newJson.json 

but it doesn't work for the path like this.

C:\Users\acer\Desktop\NewJson.json

I have searched a lot but i didn't find any proper solutions to my question. I'm using the following code to load the file from the local directory.

 <button id="loadData">update new Json</button>
 <input type="file" id="newJson" value="file" />

Here is my ajax call:

$("#loadData")[0].onclick= function (e){ 
                $.holdReady(true);
                var request = $.ajax({
                    type: "GET",
                    dataType: 'json',
                    url: $("#newJson").val(),
                    success: function (data) {
                     alert('success')
                    },
                    error: function (data, dat1, error) {
                     alert(error)
                    }
                });
            };

Any suggestions should be appreciated.

Karthi Keyan
  • 4,243
  • 5
  • 24
  • 47

2 Answers2

1

There are several reasons that this won't work:

  1. XMLHttpRequest isn't allowed access to arbitrary third party URLs (and since the URL is on the visitor's hard disk and not your website, it is a third party URL).
  2. The full path of a file input is often concealed by browsers (because the directory structure of a visitor's hard disk is none of a website's business)
  3. file:// URIs do not use exactly the same syntax as local directory paths

If you want to access files selected by the user using a file input, then use the Files API (but note limited browser support).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Also should mention that ajax isn't allowed on local files even if it's first party, not third party (both the HTML page and the json file are on the same disk, no web server). – slebetman Sep 19 '13 at 06:57
0

What browsers do you need to support? For modern browsers you can use HTML5 File API. For browsers that don't support it one option is a round-trip to a server (upload the file and return its contents), or a polyfill like https://github.com/Jahdrien/FileReader

An example using File API: (fiddle)

// check for support
if (window.File && window.FileReader && window.FileList && window.Blob) {
  // event handler to fire when file input changes
  function handleFileSelect(evt) {
    var file = evt.target.files[0],
        reader = new FileReader(),
        contents;
      if( file ){
         reader.onload = function(f){
            contents = f.target.result;
            console.log( contents ); // here you'd use JSON.parse on the contents
         };
         reader.readAsText(file);
      }
  }
  // attach the event listener. It'll fire immediately, without clicking on the other button.
  document.getElementById('newJson').addEventListener('change', handleFileSelect, false);
} else {
 console.log( 'File API not supported, sorry' )   
}

Learn more: http://www.html5rocks.com/en/tutorials/file/dndfiles/

pawel
  • 35,827
  • 7
  • 56
  • 53
  • thanks its working fine in ie 10. but it doesn't work in ie 9.. how can i use it in ie9. – Karthi Keyan Sep 20 '13 at 08:47
  • @karthik maybe you could try to find a polyfill for older browsers. Here's one, but I haven't personally tested it: https://github.com/Jahdrien/FileReader – pawel Sep 20 '13 at 08:59