2017-07-25 40 views
0

我想获取文件位置作为NW.JS中的变量。我从nw.js文档中获得了这段代码,但无法弄清楚如何使用它来返回文件位置。我是否需要编写一个脚本来使用id“fileDialog”来获取结果? https://github.com/nwjs/nw.js/wiki/file-dialogs如何获取文件位置作为NWJS中的变量

**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> 

回答

1

你最好通过文件列表input.files访问文件名:

https://github.com/nwjs/nw.js/wiki/file-dialogs看一个的LILE列表部分。

被调用函数是一个异步回调,所以你不会能够将名称返回给调用函数。只处理回调中的所有文件。

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) */ }); 
+0

完美的作品!谢谢! – Marley

相关问题