How to get the file dialogue in NW.JS to open to a folder inside of the application

867 views Asked by At

Is there any way that I can get the following code to open the file dialogue to a folder called “projects” that exists inside of the NW.JS application? I have tried the docs here... https://github.com/nwjs/nw.js/wiki/file-dialogs but haven’t found what I am looking for

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

There are 2 answers

0
AudioBubble On
<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>
0
dimacpp On

I propose more clean solution with small bugfix:

<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>