2

I'm trying to pass data (JSON) from a C# (winforms) application to a static html/javascript file that does some canvas drawing to present a graph based on details in the JSON object. The html/javascript file runs in the browser only and does not require a web server. I would like to keep from hosting the html file if possible.

Since there is no server involved, I'm assuming it's not possible to pass data via a 'get' or 'post'?

I've tried writing the JSON to a file on the user's pc, running a Process.Start() from the C# application (on the javascript file) to make it open in the browser, then having the page read the JSON data from the file. This results in a "permission denied error".

Additionally, I found the following comment: "The HTML5 fileReader facility does allow you to process local files, but these MUST be selected by the user, you cannot go rooting about the users disk looking for files." From here: How to open a local disk file with Javascript?

How can I get the data from my app to the javascript file without any user intervention? Are there any other options?

Thanks in advance for any ideas / help.

C# > Serialize the JSON, Write to File, Launch Javascript File...

...
        string json = JsonConvert.SerializeObject(this);
        sendJson(json);
    }

    private void sendJson(string json)
    {
        Assembly assy = Assembly.GetExecutingAssembly();
        string appRoot = Path.GetDirectoryName(assy.Location);
        string pathToCurveFile = string.Format("{0}\\{1}", appRoot, "WebPages\\curve.htm");
        string pathToJsonData = string.Format("{0}\\{1}", appRoot, "WebPages\\curveData.json");

        File.WriteAllText(pathToJsonData, json);
    }

Javascript > Try to Read the JSON File When Page Loads

function init() {
        var json;
        var dataFilePath = 'file://C:/Temp/curveData.json';

        var reader = new FileReader();
        reader.onload = function (e) { payload = reader.result; }
        reader.readAsText(dataFilePath);

        json = JSON.parse(payload);
Community
  • 1
  • 1
beeker
  • 780
  • 4
  • 17
  • You can try using the .net browser control and load the webpage in it, passing the data after the `#`, then get it in the js with window.location.hash. – Musa Dec 14 '15 at 21:44
  • How are you opening the html? A browser control from the winform? If so, you can add a JS event in that html and call it from C#. I can send you an example if you need it. Also you can load the page with parameters. – Manuel Dec 14 '15 at 21:47
  • From C# I am using a Process.Start(somefile.htm). The reason I stayed away from a browser control is because I am using HTML5 canvas and I didn't think the browser control would support that but, may need to research further. – beeker Dec 14 '15 at 21:49
  • Correct, it will use the IE engine of the host OS. Please check the following link to make that work with query string. `Process.Start("iexplore.exe", @"file:///D:/index.html?name=bob")` http://stackoverflow.com/questions/13550837/process-startlink-omits-part-of-the-link – Manuel Dec 14 '15 at 21:52
  • You could also look at using [CefSharp](http://cefsharp.github.io/) to embed chromium within your app. – Chris Dec 14 '15 at 21:58
  • Please let us know if it worked for you or you found another way so another person with the same or a similar scenario can use this thread for help – Manuel Dec 14 '15 at 22:13

3 Answers3

0

You best bet is to generate the whole HTML file:

  1. Read markup from your static HTML file.

  2. Inject <script> section where you have your your generated json data.

  3. Enjoy.

Riad Baghbanli
  • 3,105
  • 1
  • 12
  • 20
0

One option is to write a JavaScript file instead of plain JSON, then just add a script-tag to your HTML-page and you have access to your JSON data.

Javascript to be written, e.g. mydata.js:

var myJSON = {.....};

In HTML you just include

<script src="mydata.js" type="text/javascript"></script>

Now you can access myJSON from any script in your HTML-page.

Hope that helps.

Guido Kitzing
  • 892
  • 5
  • 14
0

You can dynamically call c# methods from html page contained in webBrowser control. For example suppose you have a class like

[ComVisible(true)] // <== Needed
public class ScriptObject
{
    public string GetJSon()
    {
        return JsonConvert.SerializeObject(new { a=1, b=2 });
        //not recommended way
        //return "{\"a\":1,\"b\":2}";
    }
}

You can call it's method in javascript like

string html =   "<html>" + 
                "<head>"+
                    "<meta http-equiv='X-UA-Compatible' content='IE=edge'>" +
                "</head>" + 
                "<script>" +
                  "var obj = JSON.parse(external.GetJSon());" +
                  "alert(obj.a)" +
                "</script>" + 
                "</html>";

webBrowser1.ObjectForScripting = new ScriptObject();
webBrowser1.DocumentText = html;

You can access the c# object in javascript using the external keyword.

Eser
  • 12,346
  • 1
  • 22
  • 32