2017-08-29 133 views
1

有沒有什麼辦法可以讓我打開文件對話框到一個名爲「projects」的文件夾中NW.JS應用程序?我在這裏嘗試了文檔... https://github.com/nwjs/nw.js/wiki/file-dialogs但沒有發現什麼,我找如何讓NW.JS中的文件對話框打開到應用程序內的文件夾

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

回答

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> 
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> 
相關問題