6

Situation:- I have created a installation setup(local) that returns a URL eg:- ved.test.com which is mapped to an IP 11.22.33.44. Thus to make the web application accessible after installation, user has to make an entry in the hosts file under "C:\WINNT\system32\drivers\etc" directory explicitly.

Approach:- After the installation application gets completed, application writes the file using Javascript.

Problem:- Writing a File using Javascript is supported in IE. I need a Solution for Firefox. Code used:-

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Write To A File</title>
<script language="javascript">
    function WriteToFile()
    {
    /* The below statement is supported in IE only */
    var fso = new ActiveXObject("Scripting.FileSystemObject");
    var s = fso.CreateTextFile("C:\\Test.txt", true);
    s.WriteLine('IE Supports Me!');
    s.Close();
    }
</script>
</head>
    <body onLoad="WriteToFile()">
</body>
</html>

Have also refered the link in SO:- How to read and write into file using JavaScript

Kindly provide a solution that supports writing a file using Javascript that runs in Firefox browser.

Thanks in Advance.

Community
  • 1
  • 1
vedvrat13
  • 466
  • 2
  • 8
  • 15
  • 1
    Your SO-link answers the question. This is not possible in the browser. – jwueller Nov 26 '10 at 10:10
  • 2
    You just overwritten my C:\Test.txt! Seriously, NO webpage is allowed to write to user's filesystem. And messing with "%SystemRoot%\system32\drivers\etc\hosts" is clearly malicious behaviour. – Free Consulting Nov 26 '10 at 10:33
  • @user205376 - Yes, i am aware of the hosts file manipulation, was assisting my app Users for better experience. Thus came across this situation. – vedvrat13 Nov 26 '10 at 10:43

4 Answers4

14

You can't do this, for hopefully obvious security reasons. JavaScript has no access to the file system...in IE it's not JavaScript, but ActiveX doing this...it just has a JavaScript API exposed.

The problem isn't that Firefox doesn't do this...it's that IE ever allowed it :)

Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
  • 4
    I am surprised that i am able to hate IE even _more_. – jwueller Nov 26 '10 at 10:02
  • IE is better, because statistical data say that the most users of `mozilla.com` use IE... – khachik Nov 26 '10 at 10:03
  • @Nick Thanks for your reply. Please suggest me a different approach if not feasible in FF. – vedvrat13 Nov 26 '10 at 10:12
  • 6
    @khachik: Why is that a reason for IE being _better_? They visit `mozilla.com` with IE to download Firefox. – jwueller Nov 26 '10 at 10:16
  • @UIDreamer - I'm not sure there is a "feasible" solution here, you're talking about an extension/codebase per browser...at that point why are you using a browser? Really you're just trying to use a browser to do something it wasn't intended to do, I'd look at another solution. For example: Flash, Silverlight, AIR, etc. – Nick Craver Nov 26 '10 at 10:18
  • @elusive It was a joke, sorry for my poor English... "IE is a very good browser, since the statistics from mozilla.com show that it is very popular." – khachik Nov 26 '10 at 10:23
3

You will need to create your own Firefox extension, because reading/writing local files is considered a privileged operation.

Reading/writing files using XPCOM: https://developer.mozilla.org/en-US/docs/Code_snippets/File_I_O. This won't work from your web page but only from privileged code such as extensions.

PCoder
  • 2,165
  • 3
  • 23
  • 32
PleaseStand
  • 31,641
  • 6
  • 68
  • 95
  • Thanks for your reply.So to run the extension, The user also needs to install these extensions? – vedvrat13 Nov 26 '10 at 10:11
  • @UIDreamer: Yes. The user would have to click on a yellow bar at the top of the window, click the Install button on the warning dialog, and then restart Firefox. – PleaseStand Nov 26 '10 at 10:13
1

While Firefox won't let you hard-code to a specific path, you can get it to present a dialog to the user who can save it to the path manually, though they will also need to change the file type (so I know this is not very practical): https://stackoverflow.com/a/13696029/271577 . The same post also shows how you can open the file contents in a new tab where the user could use the browser's save features to manually save the file (the benefit of this approach is that the file extension can be .txt by default (and it can be nice in some circumstances in that it gives the user a preview of the contents)).

Incidentally, although the native way Firefox used to have for allowing HTML to gain privileges with user permission, enablePrivilege is now out the door, I am working on an add-on called AsYouWish which allows privileged access (such as desktop file writing) to be requested of the user on a per-site basis, and though I hope it may eventually make it into Firefox (requiring a whitelist), it does currently require installation of the add-on (and it is still in alpha/beta, with a number of things to finalize or work out, most importantly enforcing the requirement to use https to avoid man-in-the-middle attacks). It currently uses an API which I think could potentially work with other browsers in the future, it informs the user of exactly what privileges are being requested, and it does not require developers to write a new add-on every time they want to have their site request privileged access from the user.

Community
  • 1
  • 1
Brett Zamir
  • 14,034
  • 6
  • 54
  • 77
1

See https://developer.mozilla.org/en-US/docs/Code_snippets/File_I_O for information on doing this in Firefox.

It looks something like this:

var file = Components.classes["@mozilla.org/file/local;1"].
       createInstance(Components.interfaces.nsILocalFile);
file.initWithPath("/home");
MGA
  • 7,575
  • 1
  • 14
  • 10
Lindsay Morsillo
  • 530
  • 6
  • 19