I am working on a completely locally run web app which is to be run without any network connection. The idea is to distribute a pack to users (~40) who will use the app to basically fill in bunch of form fields and charts. As changes are made to the page an object with data in JSON format is filled in. I have the page working well now and I have a function to load an existing JSON file and populate any fields that are present in the file. The idea behind this is that someone can be "halfway" through filling out the fields in the app or someone can load up previous work.
My question here is; what is the best system available to get data from a webpage into JSON file and be able to save that to local machine so that it can be distributed (emailed, saved to SP, network drive, access db etc etc) at a later date? The scenario under which the app will be used is a strictly no network access area so this is why I need to be able save and access a file locally. I have a half-solution where I can open a separate tab which has the contents of the file but I really would prefer to have the downloading/saving prompting done for me as most users won't go further than seeing a page with gibberish on it.
the code I have to accomplish this much is:
//Download file
function SaveToDisk() {
var data = JSON.stringify(currentSession);
var fileURL = 'data:text/json;charset=utf8,' + encodeURIComponent(data);
var fileName = $('file-save').val();
console.log(fileURL);
// for non-IE
if (!window.ActiveXObject) {
var save = document.createElement('a');
save.href = fileURL;
save.target = '_blank';
save.download = fileName || 'unknown';
var event = document.createEvent('Event');
event.initEvent('click', true, true);
save.dispatchEvent(event);
(window.URL || window.webkitURL).revokeObjectURL(save.href);
}
// for IE
else if ( !! window.ActiveXObject && document.execCommand) {
var _window = window.open(fileURL, '_blank');
_window.document.close();
_window.document.execCommand('SaveAs', true, fileName || fileURL)
_window.close();
}
}
currentSession object looks like:
//Global JSON variables
var currentSession = {
"sections":{
"header":{
"tool":"",
"subsystem":"",
"engineer":"",
"date":"",
"so":""
},
"whys":{
"why1":{
"error":"",
object:"",
norm:"",
},
why2:{
error:"",
object:"",
norm:"",
},
why3:{
error:"",
object:"",
norm:"",
},
why4:{
error:"",
object:"",
norm:"",
},
why5:{
error:"",
object:"",
norm:"",
}
},
probDescription:{
"F":{
"is":"",
"isnot":""
},
"O":{
"is":"",
"isnot":""
},
"T":{
"is":"",
"isnot":""
},
"L":{
"is":"",
"isnot":""
},
"P":"",
"W":"",
"Pos":"",
"S":""
},
possibleCauses :{
"data":""
},
notes: ""
}
};
UPDATE: Attempting this on windows machine (up to now been testing on mac) on IE9 gives the following error:
SCRIPT5007: Unable to get value of the property 'document': object is null or undefined
at the following line of the above code:
_window.document.close();
Checking up on the line before:
// for IE
else if ( !! window.ActiveXObject && document.execCommand) {
shows me that the if statement checks if activeXObject is present (only in IE???) and something else (div editable???) is true then it goes with the IE solution.
Then it creates a new window with the data url, (not sure what '_blank' is here) and attempts to save the file. So why is it erroring on the _window.document line? It does open a new window with the URL as:
data:application/octet-stream;{data}
Any help here also greatly appreciated(and needed)
All help and suggestions gladly welcomed as I am rather new to all this and still learning.
Thx in advance