Opening a file system folder/directory from web browser

淺唱寂寞╮ 提交于 2019-12-04 09:10:44

This JS code should work for IE and Firefox on Windows as long as the page was loaded from your local filesystem. You would need to test this on Linux/OSX. I don't know how you would approach chrome/safari/opera.

function execute(command, commandParam)
{

  if (isIE()) {
    try {
      activexShell = new ActiveXObject("Shell.Application");
      activexShell.ShellExecute(command, commandParam, "", "open", "1");
      exit();
    } catch (e) {
      alert("exception " + e.name + ': ' + e.message);
    }
  }
  else {
    try {
      netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
      var FileFactory = new Components.Constructor("@mozilla.org/file/local;1","nsILocalFile","initWithPath");      
      var program = new FileFactory(command);
      var process = Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces. nsIProcess);          
      process.init(program); 
      process.run(false, commandArray, commandParam.split(" ").length - 1, {});
      exit();
    } catch (e) {
      alert("exception " + e.name + ': ' + e.message);
    }
  }
}

Of course, you may need to sign the .js file in order to get it to work. For more info, see here: http://www.mozilla.org/projects/security/components/signed-scripts.html

Nope. Browsers (or most of them*) prevent this sort of behavior. They have a wall between your actual file system and the content that the Web serves you. Only the HTML input control breaks this, and they have quite a bit of protection around that, too.

*- You can use an IE ActiveX control, but that is IE-only.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!