Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/466.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 节点Webkit如何将div的值保存为.txt文件_Javascript_Html_Node.js_Node Webkit - Fatal编程技术网

Javascript 节点Webkit如何将div的值保存为.txt文件

Javascript 节点Webkit如何将div的值保存为.txt文件,javascript,html,node.js,node-webkit,Javascript,Html,Node.js,Node Webkit,我已经学习了很多教程,但我始终无法做到这一点: 我想将div的内容(启用contenteditable)保存到带有节点webkit的.txt文件中。这部分看起来像这样: <div id="editor" class="textbox" contenteditable></div> 我有一个允许我选择文件的输入字段: <input type="file" nwsaveas="untitled.txt" style="display:none;"/>

我已经学习了很多教程,但我始终无法做到这一点:

我想将div的内容(启用contenteditable)保存到带有节点webkit的.txt文件中。这部分看起来像这样:

<div id="editor" class="textbox" contenteditable></div>

我有一个允许我选择文件的输入字段:

<input type="file" nwsaveas="untitled.txt" style="display:none;"/>

但是,我找不到关于如何将editor div的值保存为用户计算机上的.txt文件的任何资源

我尝试了这个图坦卡蒙教程,简要地解释了它,但是当我在自己的项目中尝试它时,它似乎不起作用:


有人知道我是如何做到这一点的吗?

你必须打开文件对话框,模拟
点击
输入的事件,然后获取
编辑器的
内部HTML
,最后使用节点的
fs.writeFile
保存内容

以下是完整的工作示例:

<!DOCTYPE html>
<html>
<head>
<script>
var initInputFile = function() {
    document.getElementById('inputFile').addEventListener('change', function() {
        var path = this.value;   //get fullpath of chosen file
        var content = document.getElementById('editor').innerHTML;  //get editor's content
        content = (' ' + content).slice(1);   //hack to prevent strange bug of saving just half of the content
        require('fs').writeFile(path, content, function(err) {
            if (err) throw err;
            console.log('done');
        });

        var wrapper = document.getElementById('inputFileWrapper');
        wrapper.innerHTML = wrapper.innerHTML;  //hack to make "change" event trigger...
        initInputFile();                            //...when choosing the same file
    });
}
window.onload = function() {
    initInputFile();
    document.getElementById('saveBtn').addEventListener('click', function() {
        var event = document.createEvent('MouseEvents');
        event.initMouseEvent('click');
        document.getElementById('inputFile').dispatchEvent(event);
    });
}
</script>
</head>
<body>

    <div id="editor" class="textbox" style="width:400px; height:100px;" contenteditable></div>
    <div id="inputFileWrapper" style="display:none;">
        <input type="file" id="inputFile" nwsaveas="untitled.txt"/>
    </div>
    <input type="button" id="saveBtn" value="Save" />

</body>
</html>

var initInputFile=函数(){
document.getElementById('inputFile')。addEventListener('change',function(){
var path=this.value;//获取所选文件的完整路径
var content=document.getElementById('editor').innerHTML;//获取编辑器的内容
content=(''+content).slice(1);//防止只保存一半内容的奇怪错误
require('fs')。writeFile(路径、内容、函数(err){
如果(错误)抛出错误;
console.log('done');
});
var wrapper=document.getElementById('inputFileWrapper');
wrapper.innerHTML=wrapper.innerHTML;//hack使“更改”事件触发。。。
initInputFile();/…选择同一文件时
});
}
window.onload=函数(){
initInputFile();
document.getElementById('saveBtn')。addEventListener('click',function(){
var event=document.createEvent('MouseEvents');
event.initMouseEvent('click');
document.getElementById('inputFile').dispatchEvent(事件);
});
}
希望这有帮助