Javascript: Save json data as a file on the server / user's hard disk
I have developed an app that allows the user to fill out text fields with information. I want them to be able to press a button that will make a file with data (a really long array with info on what they typed and where it should go) so they can reload the data at a later date. I don't have a server now, and I am sending this app as a standalone html app to my friends for their use until I get hosing / mySql / etc.
Is there a way that when they click on a button it will take this data (saved as an array, save_data
), put it into a file, and basically begin the download process from their web-b开发者_StackOverflow中文版rowser?
And later on, what tech would I need to be looking into to save this into online user accounts?
It is posible, but only if you force your users to use windows and microsoft internet explorer. You can send the html file as a hta file, wich can write and read from data from the hard disk.
check http://msdn.microsoft.com/en-us/library/ms536496%28VS.85%29.aspx
for more information.
An hta file is basically a local html file with some extra tags in the header, and is run (interpreted) locally without security restrictions, like any exe file.
I don't know how to show the complete html code here,(markdown is not for me), so if you want an example:
1 - create a file test.hta, with the standard html, head, body and script tags
2 - inside head tag, insert
<HTA:APPLICATION
ID="oMyApp"
APPLICATIONNAME="test"
BORDER="yes"
CAPTION="yes"
SHOWINTASKBAR="yes"
SINGLEINSTANCE="yes"
SYSMENU="yes">
</HTA:APPLICATION>
3 - inside body tab put a button with onclick="writeText();"
4 - inside the script tag insert
function writeText(){
try{
var fso = new ActiveXObject("Scripting.FileSystemObject");
var fileObject = fso.OpenTextFile("C:\\testhta.txt", 8, true,0);
fileObject.WriteLine('text file written');
fileObject.close();
}catch(ex){
alert(ex);
}
}
5 - save it, doubleclick it, click on the button and you will get a nice "C:\testhta.txt" file with 'text file written' in it.
If you don't have a server, then no ... there is no way to initiate a file download/save based on dynamically generated data with javascript
精彩评论