在 Firefox 中,使用 Javascript 写入文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4284199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In Firefox, Write to a File using Javascript?
提问by vedvrat13
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.
情况:- 我创建了一个安装设置(本地),它返回一个 URL,例如:- ved.test.com,它映射到 IP 11.22.33.44。因此,为了在安装后可以访问 Web 应用程序,用户必须在“C:\WINNT\system32\drivers\etc”目录下的 hosts 文件中明确创建一个条目。
Approach:- After the installation application gets completed, application writes the file using Javascript.
方法:- 安装应用程序完成后,应用程序使用 Javascript 写入文件。
Problem:- Writing a File using Javascript is supported in IE. I need a Solution for Firefox. Code used:-
问题:- IE 支持使用 Javascript 编写文件。我需要一个Firefox解决方案。使用的代码:-
<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
还参考了 SO 中的链接:- How to read and write into file using JavaScript
Kindly provide a solution that supports writing a file using Javascript that runs in Firefoxbrowser.
请提供一个支持使用在Firefox浏览器中运行的 Javascript 编写文件的解决方案。
Thanks in Advance.
提前致谢。
回答by Nick Craver
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.
出于明显的安全原因,您不能这样做。JavaScript 无法访问文件系统......在 IE 中它不是 JavaScript,而是 ActiveX 这样做......它只是公开了一个 JavaScript API。
The problem isn't that Firefox doesn't do this...it's that IE ever allowed it :)
问题不在于 Firefox 不这样做……而是 IE 曾经允许这样做:)
回答by PleaseStand
You will need to create your own Firefox extension, because reading/writing local files is considered a privileged operation.
您需要创建自己的 Firefox 扩展,因为读取/写入本地文件被视为特权操作。
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.
使用 XPCOM 读取/写入文件:https: //developer.mozilla.org/en-US/docs/Code_snippets/File_I_O。这不适用于您的网页,而只能适用于特权代码,例如扩展程序。
回答by Lindsay Morsillo
See https://developer.mozilla.org/en-US/docs/Code_snippets/File_I_Ofor information on doing this in Firefox.
有关在 Firefox 中执行此操作的信息,请参阅https://developer.mozilla.org/en-US/docs/Code_snippets/File_I_O。
It looks something like this:
它看起来像这样:
var file = Components.classes["@mozilla.org/file/local;1"].
createInstance(Components.interfaces.nsILocalFile);
file.initWithPath("/home");
回答by Brett Zamir
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)).
虽然 Firefox 不会让您硬编码到特定路径,但您可以让它向用户显示一个对话框,用户可以手动将其保存到路径,尽管他们还需要更改文件类型(所以我知道这一点不太实用):https: //stackoverflow.com/a/13696029/271577。同一篇文章还展示了如何在新选项卡中打开文件内容,用户可以在该选项卡中使用浏览器的保存功能手动保存文件(这种方法的好处是默认情况下文件扩展名可以是 .txt(并且它在某些情况下可能会很好,因为它可以让用户预览内容))。
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 AsYouWishwhich 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.
顺便说一句,虽然 Firefox 过去允许 HTML 通过用户许可获得特权的本机方式,但enablePrivilege现在已经不在了,我正在开发一个名为AsYouWish的附加组件它允许在每个站点的基础上向用户请求特权访问(例如桌面文件写入),尽管我希望它最终可以进入 Firefox(需要白名单),但它目前确实需要安装 add- (并且它仍处于 alpha/beta 阶段,有许多事情需要完成或解决,最重要的是强制要求使用 https 以避免中间人攻击)。它目前使用一个 API,我认为将来可能会与其他浏览器一起使用,它会准确地通知用户正在请求哪些权限,并且不需要开发人员每次想要拥有他们的权限时都编写新的附加组件站点请求用户的特权访问。

