Tuesday, May 21, 2024
 Popular · Latest · Hot · Upcoming
71
rated 0 times [  76] [ 5]  / answers: 1 / hits: 42249  / 14 Years ago, mon, february 28, 2011, 12:00:00

I got a requirement as following:



There is a link on a web page. As user clicks on link it should create a file on the fly and a download box pops up. How to do it using java script?


More From » xml

 Answers
13

If the user trusts you, you can can create XML file directly in his filesystem.
Example code for Mozilla Firefox:



function mozillaSaveFile(filePath,content)
{
if(window.Components) {
try {
netscape.security.PrivilegeManager.enablePrivilege(UniversalXPConnect);
var file = Components.classes[@mozilla.org/file/local;1].createInstance(Components.interfaces.nsILocalFile);
file.initWithPath(filePath);
if(!file.exists())
file.create(0,0664);
var out = Components.classes[@mozilla.org/network/file-output-stream;1].createInstance(Components.interfaces.nsIFileOutputStream);
out.init(file,0x20|0x02,00004,null);
out.write(content,content.length);
out.flush();
out.close();
return true;
} catch(ex) {
return false;
}
}
return null;
}


if you need support for all browsers, see how it is implemented in http://www.tiddlywiki.com



EDIT: This doesn't work for Firefox 17+ because changing privileges was deemed unsafe and removed. see here for more details: https://bugzilla.mozilla.org/show_bug.cgi?id=546848#c57


[#93534] Saturday, February 26, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lizette

Total Points: 252
Total Questions: 91
Total Answers: 103

Location: Sint Maarten
Member since Tue, Mar 29, 2022
2 Years ago
;