如何在NW.JS中获取文件对话框以打开应用程序内的文件夹

时间:2017-08-29 15:20:55

标签: javascript node-webkit nw.js

有没有办法让我可以获得以下代码来打开文件对话框到NW.JS应用程序中存在的名为“projects”的文件夹?我在这里尝试了文档...... https://github.com/nwjs/nw.js/wiki/file-dialogs但是没找到我要找的东西

<input id="fileDialog" type="file">
<script>
document.querySelector('#fileDialog')
  .addEventListener("change", function() {
    var filePath = this.value;
    alert(filePath);
  });
</script>

2 个答案:

答案 0 :(得分:0)

<label for="fileDialogOPEN"><img src="../images/open.png" style="width:20px;background:none;"></label>
<div id="openBUT"></div>

<script>
 var nwDir = process.cwd() + "/projects";
 var goForIt = '<input id="fileDialogOPEN" nwworkingdir="' + nwDir + '" type="file" name="photo" style="display: none;" >';
  $("#openBUT").append(goForIt);

  document.querySelector('#fileDialogOPEN')
   .addEventListener("change", function() {
    var filePath = this.value;
    alert(filePath);
  });
</script>

答案 1 :(得分:0)

我建议使用小错误修复更简洁的解决方案:

<input id="opendlg" type="file" accept=".*" />

<script>
opendlg.setAttribute('nwworkingdir', require('path').join(process.cwd(), 'projects'));
opendlg.addEventListener('change', function (e) {
  var filepath = e.target.value;
  if (filepath) {
    e.target.value = ''; // or you will not receive change-event next time on the same file
    alert(filepath);
  }
});
</script>