自动在Firefox中保存文件 [英] Auto Save a file in Firefox
问题描述
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);
但问题在于它会打开一个对话框来确认本地驱动器的位置细节。有没有办法我可以硬编码本地驱动器的存储位置,并自动保存文件?
然后 FileUtils 和 NetUtil.asyncCopy 是您的好友: Components.utils.import(resource://gre/modules/FileUtils.jsm);
Components.utils.import(resource://gre/modules/NetUtil.jsm);
var TEST_DATA =这是一个测试字符串;
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);
这将异步写入字符串,这是一个测试字符串
放入文件 c:\foo\bar.txt
中。请注意, NetUtil.asyncCopy
会自动关闭这两个流,您不需要这样做。但是,您可能希望将一个函数作为第三个参数传递给此方法 - 写入操作完成时将调用它。
另请参阅:代码片断,写入文件
I am trying to find a way where by we can auto save a file in Firefox using JS. The way I have done till yet using FireShot on a Windows Desktop:
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:
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
. Note that NetUtil.asyncCopy
closes both streams automatically, you don't need to do it. 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
这篇关于自动在Firefox中保存文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!