gpt4 book ai didi

javascript - 如何在 NWJS 中获取文件位置作为变量

转载 作者:行者123 更新时间:2023-12-03 03:47:43 24 4
gpt4 key购买 nike

我正在尝试获取文件位置作为 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>

最佳答案

您最好通过文件列表访问文件名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) */ });

关于javascript - 如何在 NWJS 中获取文件位置作为变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45302625/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com