如何使用JavaScript编写和阅读文本文件?
答案 0 :(得分:6)
您需要在提供用于访问文件系统的API的主机环境中运行JS。
如果您使用的是Windows,请you can use WSH to achieve this。
在正常安全条件下运行浏览器的JS无法访问文件系统。
答案 1 :(得分:6)
如果您使用的是Firefox,这可能会有所帮助。
//Your text file location on system
var savefile = "c:\\yourtextfile.txt";
try {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
var file = Components.classes["@mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsILocalFile);
file.initWithPath( savefile );
if ( file.exists() == false ) {
alert( "Creating file... " );
file.create( Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 420 );
}
var outputStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
.createInstance( Components.interfaces.nsIFileOutputStream );
outputStream.init( file, 0x04 | 0x08 | 0x20, 420, 0 );
var output = "Your text here";
var result = outputStream.write( output, output.length );
outputStream.close();
alert("Done");
}
catch (e) {
alert("Some error occured");
}
它对我有用,希望也适合你:)
答案 2 :(得分:4)
你做不到。根据设计,浏览器中的JavaScript无法访问用户的文件系统。
答案 3 :(得分:2)
有一个有趣的脚本,如果你愿意使用greasemonkey:
// ==UserScript==
// @name Store notes for every website
// @creator Xavi Esteve
// @namespace http://www.xaviesteve.com
// @description Shows a little notebook at the right bottom of every page that stores any text you type in automatically. Each domain has his separate notebook which can be shown/hidden with a click.
// @version 1.3
// @include *
// @exclude http*://*.google.*/mail/*
// @exclude http*://*.googlemail.*
// @exclude file:///*
// ==/UserScript==
if (self == top) {
// VARIABLES
var e = document.domain.split(/\./);
gdomain = document.domain;
var gotit = GM_getValue(gdomain, '[Type notes for '+gdomain+']');
// FUNCTIONS
function saveit() {
GM_setValue(gdomain, document.getElementById('gm_textarea').value);
return false;
}
/* Insert HTML */
/* div */
var div = document.createElement('div');
div.innerHTML = '<a onmousedown="var tbh = document.getElementById(\'gm_tobehiden\');if(tbh.style.display==\'none\'){tbh.style.display=\'block\';document.getElementById(\'gm_textarea\').focus();}else{tbh.style.display = \'none\';}return false;" title="Notebook">'+gdomain+'</a><div id="gm_tobehiden"><div id="gm_title"></div></div>';
div.id = "gm_notebook";
document.body.insertBefore(div, document.body.lastChild);
/* textarea */
var textarea = document.createElement('textarea');
textarea.appendChild(document.createTextNode(gotit));
textarea.addEventListener('keyup', saveit, false);
textarea.addEventListener('click', saveit, false);
textarea.id = "gm_textarea";
var gm_title = document.getElementById('gm_title');
gm_title.parentNode.insertBefore(textarea, gm_title.nextSibling);
/* Insert CSS */
var menuCode = new Array();
menuCode.push("#gm_notebook {-moz-opacity:0.9;position:fixed;bottom:40px;right:5px;border:1px solid #ccc;font-size:10px;color:#333;background:#f1f1f1;padding:3px 5px 5px 5px;font-family:Arial,sans-serif}#gm_notebook a {color:#0085d5;margin:2px;cursor:pointer}");
menuCode.push("#gm_tobehiden {display:none;width:200px;height:300px;padding:5px}"); // Change display to block to show the notebook by default
menuCode.push("#gm_textarea {width:100%;height:100%;color:#000;font-family:monospace}");
var style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = menuCode.join('');
menuCode.length = 0;
try { document.getElementsByTagName('head')[0].appendChild(style); }
catch(e) {}
}
答案 4 :(得分:0)
您无法使用Java Script访问您的文件系统,所以很遗憾,您无法
答案 5 :(得分:0)
在FF 3.6中,有可能,请参阅http://www.bruechner.de/md5file/js/
上的技术示例