我想使用图像作为按钮在NW.JS中打开文件对话框,我该怎么做?
我有这个 HTML
<button id="open" style="background: none;"><img src="images/open.png" style="width:20px;background:none;"></button></div>
<input style="display:none;" id="fileDialog" type="file" />
这个 JS
function chooseFile(name, handleFile) {
const chooser = document.querySelector(name);
chooser.onchange = function () {
for (const f of this.files) {
console.log(f.name);
console.log(f.path);
handleFile(f.name, f.path);
}
};
chooser.click();
}
chooseFile('#fileDialog', function(name, path){ ... /* do something with the file(s) */ });
答案 0 :(得分:0)
这是完整的工作示例:
<script>
openbtn.addEventListener('click', e => opendlg.click());
opendlg.addEventListener('change', e => {
let files = e.target.value;
if (files) {
e.target.value = ''; // or you will not receive change-event next time on the same files
files.split(';').forEach(filepath => {
alert(filepath);
});
}
});
</script>