聘我网

新概念招聘3.0

Javascript如何保存文件到文件系统?

vote up0vote downstar

rt,发现http://www.tiddlywiki.com/有这个功能

 

1 个答复

vote up0vote downcheck

看下代码,在firefox中是这样实现的:

function mozillaSaveFile(filePath,content)
{
    if(window.Components) {
        try {
            netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
            var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
            file.initWithPath(filePath);
            if(!file.exists())
                file.create(0,0664);
            var out = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
            out.init(file,0x20|0x02,00004,null);
            out.write(content,content.length);
            out.flush();
            out.close();
            return true;
        } catch(ex) {
            return false;
        }
    }
    return null;
}

mozillaSaveFile('C:\\Documents and Settings\\test.txt','content');

这种操作必须在本地访问时(file:)才可能成功。

其他情况可以看其中saveFile的实现:

function saveFile(fileUrl,content)
{
    var r = mozillaSaveFile(fileUrl,content);
    if(!r)
        r = ieSaveFile(fileUrl,content);
    if(!r)
        r = javaSaveFile(fileUrl,content);
    return r;
}
链接

您的回答





不是您要找的问题? 浏览其他含有标签 的问题或者 自己问个.