Reputation: 47
I understand javascript is not designed for file manipulation due to security concerns. However, recently I figured out a way to read files using firefox. Is there a way to write to a file as well?
I found the following two potential solutions:
However, since I am extremely inexperienced in this matter I did not figure out how to use either of them. Could anyone provide a very basic example please?
Thanks
Edit: I tried to use 1. to write to a file, but it work here is what I wrote:
My firefox is a clean install of Firefox 8, hence there shouldn't be any addon conflicts I suppose.
Upvotes: 2
Views: 10670
Reputation: 71
function WriteToFile()
{
var fso = new ActiveXObject("Scripting.FileSystemObject");
var s = fso.CreateTextFile("C:\\NewFile.txt", true);
var text=document.getElementById("TextArea1").innerTex;
s.WriteLine(text);
s.Close();
}
You can write text to a file and save it in the local drive. A very basic example is as follows,
The TextArea1 is the id of a form element which contains the target text. The function can be called in a button click event.
Hope that helps!
Upvotes: 1
Reputation: 3895
You won't be able to access the XPCOM components from non-privileged code.
What that means in English is that those APIs are only available to things like addons running on the browser, not on website code.
There is no way (and will never be a way for security reasons) for a website to read/write local files, apart from cookies, without some plugin like flash providing a way of doing it.
Upvotes: 2