如何让NW.JS中的文件对话框打开到应用程序内的文件夹
问题描述:
有没有什么办法可以让我打开文件对话框到一个名为“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>
答
<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>
答
我提出更为清晰的解决方案有小修正:
<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>