我的应用程序是用占位符构建的,当点击“加载”时,文件会被直接读取。
<button id="loadbutton" type="button" class="btn btn-success" onclick="showTheFile()">Load</button></a>showthefile()做了一些事情,然后打电话给...
var keyMapLoc = '\\path\\to\\file.txt';
function readKeys(ffile) {
// read the keyfile
var ffile = ffile || keyMapLoc;
return fs.readFileSync(ffile, 'utf8');
}这会将文件读取到解析它的应用程序yotta yotta中。
I followed these instructions,并使用了演示。应用程序一打开,文件对话框就会弹出,这是我得到的。
<html>
<body>
<input style="display:none;" id="fileDialog" type="file" />
<script>
function chooseFile(name) {
var chooser = document.querySelector(name);
chooser.addEventListener("change", function(evt) {
console.log(this.value);
}, false);
chooser.click();
}
chooseFile('#fileDialog');
</script>
</body>
</html>然而,即使我知道如何使文件对话框弹出,也知道如何读取/解析文件,我仍然很难将这个非常抽象的示例应用到我现有的nwjs应用程序中。
基于我的应用程序的上述示例,我应该如何混合在演示中,才能使“加载”按钮在加载文件时按预期操作?
发布于 2016-01-26 06:10:40
由于您没有提供您的代码,因此我将停止演示。您需要做的是触发文件输入元素的click事件,然后在发生change事件时调用readKeys()。
<!DOCTYPE html>
<html>
<body>
<input style="display:none;" id="fileDialog" type="file"/>
<button id="loadButton" type="button" class="btn btn-success"
onclick="showTheFile()">Load</button>
<script>
var fs = require('fs');
var keyMapLoc = '\\path\\to\\file.txt';
var chooser = document.querySelector("#fileDialog");
// Set up the file chooser for the on change event
chooser.addEventListener("change", function(evt) {
// When we reach this point, it means the user has selected a file,
// so invoke readKeys().
// this.value contains the path to the selected file
console.log(readKeys(this.value));
}, false);
function showTheFile() {
// Trigger click event on the chooser, this will bring up the
// dialog
chooser.click()
}
function readKeys(ffile) {
var ffile = ffile || keyMapLoc;
return fs.readFileSync(ffile, 'utf8');
}
</script>
</body>
</html>https://stackoverflow.com/questions/35001769
复制相似问题