2009-12-12 8 views

Répondre

6

Vous devez exécuter JS dans un environnement hôte qui fournit une API pour accéder au système de fichiers.

Si vous utilisez Windows, alors you can use WSH to achieve this. JS exécutant un navigateur, dans des conditions de sécurité normales, ne peut pas accéder au système de fichiers.

0

Vous ne pouvez pas accéder à votre système de fichiers avec Java Script, donc, malheureusement, vous ne pouvez pas

+0

Dans un * navigateur Web *. JavaScript n'est pas limité aux navigateurs Web. –

+0

@Crowder: Techniquement, vous avez raison, mais je le suppose * de facto * sauf indication contraire explicite. Je suppose qu'une application .NET est en cours d'exécution sur Windows, sauf si vous me dites qu'il fonctionne sur Mono. –

+3

@ dnl.vssll: Ne supposez jamais, il fait un "cul" de "u" et "moi". –

4

Vous ne pouvez pas. JavaScript dans le navigateur n'a pas accès au système de fichiers de l'utilisateur, par conception.

6

Si vous utilisez Firefox, cela peut aider.

//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"); 
} 

Il a travaillé pour moi, des œuvres d'espoir pour vous aussi :)

+0

Fonctionne pour moi. Merci. Est-ce que Chrome a cette capacité? –

2

il y a un script intéressant, dans le cas où vous êtes prêt à utiliser 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) {} 

} 
Questions connexes