简体   繁体   English

自动在Firefox中保存文件

[英]Auto Save a file in Firefox

I am trying to find a way where by we can auto save a file in Firefox using JS. 我正在尝试找到一种方法,使我们可以使用JS在Firefox中自动保存文件。 The way I have done till yet using FireShot on a Windows Desktop: 我在Windows桌面上仍使用FireShot之前所做的工作:

var element = content.document.createElement("FireShotDataElement");
element.setAttribute("Entire", EntirePage);
element.setAttribute("Action", Action);
element.setAttribute("Key", Key);
element.setAttribute("BASE64Content", "");
element.setAttribute("Data", Data);
element.setAttribute("Document", content.document);
if (typeof(CapturedFrameId) != "undefined")
  element.setAttribute("CapturedFrameId", CapturedFrameId);
content.document.documentElement.appendChild(element);
var evt = content.document.createEvent("Events");
evt.initEvent("capturePageEvt", true, false);
element.dispatchEvent(evt);

But the issue is that it opens a dialog box to confirm the local drive location details. 但是问题在于它会打开一个对话框,以确认本地驱动器的位置详细信息。 Is there a way I can hard code the local drive storage location and auto save the file? 有什么方法可以对本地驱动器的存储位置进行硬编码并自动保存文件?

If you are creating a Firefox add-on then FileUtils and NetUtil.asyncCopy are your friends: 如果你正在创建一个Firefox插件则文件实用程序NetUtil.asyncCopy是您的朋友:

Components.utils.import("resource://gre/modules/FileUtils.jsm");
Components.utils.import("resource://gre/modules/NetUtil.jsm");

var TEST_DATA = "this is a test string";
var source = Components.classes["@mozilla.org/io/string-input-stream;1"].
                 createInstance(Components.interfaces.nsIStringInputStream);
source.setData(TEST_DATA, TEST_DATA.length);

var file = new FileUtils.File("c:\\foo\\bar.txt");
var sink = file.openSafeFileOutputStream(file, FileUtils.MODE_WRONLY |
                                               FileUtils.MODE_CREATE);
NetUtil.asyncCopy(source, sink);

This will asynchronously write the string this is a test string into the file c:\\foo\\bar.txt . 这会将异步的字符串( this is a test string写入文件c:\\foo\\bar.txt Note that NetUtil.asyncCopy closes both streams automatically, you don't need to do it. 请注意, NetUtil.asyncCopy自动关闭两个流,您无需这样做。 However, you might want to pass a function as third parameter to this method - it will be called when the write operation is finished. 但是,您可能希望将函数作为第三个参数传递给该方法-写操作完成后将调用该函数。

See also: Code snippets, writing to a file 另请参阅: 代码段,写入文件

Every computer has a different file structure. 每台计算机都有不同的文件结构。 But still, there is a way. 但是,仍然有办法。 You can save it to cookie / session, depends on how "permanent" your data wants to be. 您可以将其保存到Cookie /会话中,具体取决于您的数据希望有多“永久”。

Do not consider writing a physical file as it requires extra permission. 不要考虑写入物理文件,因为它需要额外的权限。

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

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