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