我正在尝试将文件位置作为NW.JS中的变量。我有来自nw.js文档的代码,但无法弄清楚如何使用它来返回文件位置。我是否需要编写脚本来使用id“fileDialog”来获得结果? https://github.com/nwjs/nw.js/wiki/file-dialogs
**HTML**
<input style="display:none;" id="fileDialog" type="file" />
**Javascript**
<script>
function chooseFile(name) {
var chooser = document.querySelector(name);
chooser.addEventListener("change", function(evt) {
console.log(this.value);
}, false);
chooser.click();
}
chooseFile('#fileDialog');
</script>
答案 0 :(得分:1)
您最好通过文件列表input.files
访问文件名:
https://github.com/nwjs/nw.js/wiki/file-dialogs查看文章列表部分。
被调用的函数是异步回调,因此您无法将名称返回给调用函数。只需处理回调中的所有文件。
function chooseFile(name, handleFile) {
var chooser = document.querySelector(name);
chooser.addEventListener("change", function(evt) {
for(var f of this.files){
console.log(f.name);
console.log(f.path);
handleFile(f.name, f.path);
}
}, false);
chooser.click();
}
chooseFile('#fileDialog', function(name, path){ ... /* do something with the file(s) */ });