admin管理员组

文章数量:1434914

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 (easy)
  2. Read the local JSON file with JavaScript and produce an html page with useful graphs etc (using Raphaeljs).
  3. Deliver the oute (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.

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 (easy)
  2. Read the local JSON file with JavaScript and produce an html page with useful graphs etc (using Raphaeljs).
  3. Deliver the oute (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.

Share Improve this question edited Dec 10, 2012 at 17:18 Fenton 252k73 gold badges402 silver badges410 bronze badges asked Dec 10, 2012 at 15:10 theohartheohar 1173 silver badges8 bronze badges 3
  • I'm not 100% sure I understood your setup. Is the .NET program that generates the JSON file a GUI app / service running on the client machine? Will you launch the browser for the user, or is the user expected to choose the file on a pre-existing page? – jevakallio Commented Dec 10, 2012 at 15:17
  • by "local" do you mean a file on the webserver? or on the end user's machine?... if the file is on the server you can make this work... if on the client you're stuck. – scunliffe Commented Dec 10, 2012 at 15:22
  • The generates the JSON file containg my data. Then the json should be read by javascript to produce graphs, etc. What I return to the client is the html (with javascript and json) in order to see visually my data (client does not have to provide any data or json). You can imagine that the html acts as a report back to the client instead of a presentation, let say. Everything is local and static. – theohar Commented Dec 10, 2012 at 15:23
Add a ment  | 

3 Answers 3

Reset to default 1

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.

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.

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).

本文标签: Read local JSON file (no webserver) with JavaScriptStack Overflow