简体   繁体   English

如何在Mozilla Firefox中使用javascript创建文件

[英]How to create a file using javascript in Mozilla Firefox

I want to write a function in javascript which creates a file and write some content to it, iam working with firefox, can anybody help me in this case. 我想在javascript中编写一个函数,它创建一个文件并写一些内容给它,我在使用firefox,在这种情况下,任何人都可以帮助我。

Thanks... 谢谢...

You can write files in JavaScript in Firefox, but you have to use an XPCOM object (internal browser API). 您可以在Firefox中使用JavaScript编写文件,但必须使用XPCOM对象(内部浏览器API)。 This is not allowed for JavaScript loaded from a web page, and it is intended to be used by JavaScript running inside a Firefox add-on (with high level of privileges). 对于从网页加载的JavaScript,不允许这样做,并且它旨在由在Firefox附加组件中运行的JavaScript(具有高级别权限)使用。

There is a way for unprivileged (web page) JavaScript to request more privileges and if the user grants it (there will be a pop up dialog asking for permission), the web page code would be able to write to a file. 有一种方法可以让非特权(网页)JavaScript请求更多权限,如果用户授予它(会有一个弹出对话框要求权限),网页代码就能写入文件。

But before you read further, a warning: 但在你进一步阅读之前,警告:

This is not standard JavaScript and I would not recommend this approach unless you are developing a very specific application, that will be used in a very specific way (like for example, http://www.tiddlywiki.com/ a client-side JavaScript-HTML only wiki). 这不是标准的JavaScript,我不推荐这种方法,除非你正在开发一个非常特定的应用程序,它将以一种非常具体的方式使用(例如, http ://www.tiddlywiki.com/客户端JavaScript -HTML only wiki)。

Requesting XPCOM privileges on a website is a bad practice! 在网站上请求XPCOM权限是一种不好的做法! It's basicly equivalent to running an .exe you just downloaded from a site. 它基本上等同于运行您刚从站点下载的.exe。 You are asking a user to grant full access to their computer (read, write, execute) with the identity of the user running Firefox. 您要求用户使用运行Firefox的用户的身份授予对其计算机的完全访问权限(读取,写入,执行)。

Request permission to use XPCOM (this will prompt the user for confirmation, no way to avoid it): 请求使用XPCOM的权限(这将提示用户进行确认,无法避免):

netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

Then, write to a file using an XPCOM object (example code from Mozilla Developer Network): 然后,使用XPCOM对象(Mozilla Developer Network的示例代码)写入文件:

   1. // file is nsIFile, data is a string  
   2. var foStream = Components.classes["@mozilla.org/network/file-output-stream;1"].  
   3.                          createInstance(Components.interfaces.nsIFileOutputStream);  
   4.   
   5. // use 0x02 | 0x10 to open file for appending.  
   6. foStream.init(file, 0x02 | 0x08 | 0x20, 0666, 0);   
   7. // write, create, truncate  
   8. // In a c file operation, we have no need to set file mode with or operation,  
   9. // directly using "r" or "w" usually.  
  10.   
  11. // if you are sure there will never ever be any non-ascii text in data you can   
  12. // also call foStream.writeData directly  
  13. var converter = Components.classes["@mozilla.org/intl/converter-output-stream;1"].  
  14.                           createInstance(Components.interfaces.nsIConverterOutputStream);  
  15. converter.init(foStream, "UTF-8", 0, 0);  
  16. converter.writeString(data);  
  17. converter.close(); // this closes foStream  

You can find more information about I/O in Firefox using XPCOM here: https://developer.mozilla.org/en-US/docs/Code_snippets/File_I_O 您可以在此处使用XPCOM在Firefox中找到有关I / O的更多信息: https//developer.mozilla.org/en-US/docs/Code_snippets/File_I_O

Javascript from websites cannot access the local file system. 来自网站的Javascript无法访问本地文件系统。

If you like to store data either store it on the server or in a cookie. 如果您想存储数据,请将其存储在服务器或cookie中。

writing to the file system directly from a browser is prohibited for security reasons. 出于安全原因,禁止直接从浏览器写入文件系统。 With html5 however it'll be possible to have offline storage support. 但是使用html5,可以获得离线存储支持。 Take a look here . 看看这里

Grz, Kris. 格兹,克里斯。

Javascript executes in a client-side context. Javascript在客户端上下文中执行。

http://www.tek-tips.com/viewthread.cfm?qid=1171273&page=1 http://www.tek-tips.com/viewthread.cfm?qid=1171273&page=1

There will be an API for this.. File Writer API. 这个..文件编写器API将有一个API。 The early specification is here: http://www.w3.org/TR/file-writer-api/ It is not implemented in any browser yet. 早期规范如下: http//www.w3.org/TR/file-writer-api/它尚未在任何浏览器中实现。

Update: It seems there already exists an implementation. 更新:似乎已经存在一个实现。 Check out http://caniuse.com/filesystem and http://www.html5rocks.com/en/tutorials/file/filesystem/ 查看http://caniuse.com/filesystemhttp://www.html5rocks.com/en/tutorials/file/filesystem/

While everyone who's responded that javascript does not have the ability to write files on a remote server are correct, and this is true for security reasons, what you want to accomplish may still be possible. 虽然每个回复javascript都没有能力在远程服务器上写文件的人都是正确的,但出于安全原因这是正确的,你想要完成的事情仍然是可能的。

For example, if you wanted to make it possible to create a file on your website with the use of javascript, you can do so with some server side scripting language and and AJAX call. 例如,如果您希望使用javascript在网站上创建文件,则可以使用某些服务器端脚本语言和AJAX调用来实现。

Example: 例:

You have a file on your server called update_last_access.php which will create a file which stores the last time the file was accessed in some arbitrary file. 您的服务器上有一个名为update_last_access.php的文件,它将创建一个文件,用于存储上次在某个任意文件中访问文件的时间。

If you then had your javascript function make an AJAX call out to that script, for instance, in jquery 如果你有你的javascript函数对该脚本进行AJAX调用,例如,在jquery中

$.get("update_last_access.php")

Then this would execute the server side script and write to the file. 然后,这将执行服务器端脚本并写入该文件。

Before any more help can be provided for you, you're going to have to clarify what you're trying to do. 在为您提供更多帮助之前,您必须澄清您要做的事情。

You can read files from the filesystem in JavaScript with Firefox 3.6 - see my EPUB reader proof of concept , for example. 您可以使用Firefox 3.6从JavaScript文件系统中读取文件 - 例如,请参阅我的EPUB读者概念验证

You can't write files directly from JavaScript, though. 但是,您无法直接从JavaScript编写文件。 You have to go via a server. 你必须通过服务器。

Mozilla计划将FileSaver包含在Gecko 9中: https ://bugzilla.mozilla.org/show_bug.cgi?id = 557540

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM