theohar
theohar

Reputation: 117

Read local JSON file (no web-server) with JavaScript

It seems that this has been asked a number of times, but I would like to make sure there are still no other alternatives.

So what I need to do is:

  1. Generate a .JSON file from .net (easy)
  2. Read the local JSON file with JavaScript and produce an html page with useful graphs etc (using Raphaeljs).
  3. Deliver the outcome (HTML + associated JavaScript) to the client.

I wouldn't mind installing a local Apache to read the JSON file easily but since I cannot ask the client to do this, I am looking for a way to read a local JSON file (on the filesystem, no web server) with JavaScript, so that I eventually provide the client with the HTML, .js and .json and he can see the associated graphs.

I 've been reading that FileReader class is an option but I want to have the flexibility to support these on a number of IE browsers (let say IE7 to IE10).

Any help on how to work round this would be much appreciated.

Upvotes: 7

Views: 4757

Answers (3)

jevakallio
jevakallio

Reputation: 35970

Web browsers are not able to read files from the file system due to security reasons. If I've understood correctly, you have a .NET application running on the local machine?

If this is the case, you can host a WCF service to serve the JSON from your application. WCF self-hosting is described on this MSDN page. You should be able to make AJAX requests from the browser to a server hosted within you application on localhost.

Upvotes: 1

Viktor S.
Viktor S.

Reputation: 12815

You can't read file from local disk in browser (security restriction). But once you are going to deliver HTML + js and JSON (in zip?) you can read json like from regular web server. Just use relative links and if JSON will be in same folder where HTML and JS is located. Or you may put your JSON as a part of HTML you deliver to customer.

But if you are going to show a page (HTML and JS) on your site in web, you should better ask client to upload their JSON file, and than download it from server (in AJAX style).

Upvotes: 0

epascarello
epascarello

Reputation: 207557

If you want to support older IE, you need to go into ActiveX Security setting hell. Look into var fso = new ActiveXObject("Scripting.FileSystemObject");

But if you are delivering the content and HTML files to the client, why don't you just add the JSON markup directly into the html document? Seems like the EASY solution.

Upvotes: 0

Related Questions