说明:该例子主要实现把输入框中的文字保存到本地的文本文档中。

在main中添加几句代码

const ipcMain = electron.ipcMain;
const dialog = electron.dialog;
ipcMain.on("openDir", function (e) {
var fileName = dialog.showOpenDialog(mainWindow, {title: "选择一个目录", properties: ["openDirectory"]});
e.returnValue = fileName ? fileName : null;
});

这段代码防止文件没有名字而出错

html代码

 <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
</head>
<body>
<button id="btn-select-dest-dir">选择保存的目录</button>
<form id="form-data">
<div>
<textarea name="txt" style="width: 300px;height: 200px" required="required"></textarea>
</div>
<div>
<input type="submit" value="保存">
</div>
</form>
</body> <script>
// You can also require other files to run in this process
require('./renderer.js')
</script>
</html>

renderer代码

 // This file is required by the index.html file and will
// be executed in the renderer process for that window.
// All of the Node.js APIs are available in this process.
const {ipcRenderer}=require("electron");
const fs = require("fs"); class Renderer {
constructor() {
this.btnSelectDestDir = document.getElementById("btn-select-dest-dir");
this.formSaveData = document.getElementById("form-data"); this.addListeners();
} addListeners() {
var self = this; this.btnSelectDestDir.onclick = function () {
var files = ipcRenderer.sendSync("openDir"); if (files && files.length) {
self.destDir = files[0];
}
}; this.formSaveData.onsubmit = function (e) {
e.preventDefault(); if (!self.destDir) {
alert("请选择要保存的目录");
return;
} var destFile = `${self.destDir}/data.txt`;
fs.writeFile(destFile, this["txt"].value, function (err) {
if (!err) {
alert(`成功保存文件${destFile}`);
} else {
alert("无法保存文件");
}
})
};
}
}
new Renderer();

运行即可

补充:在main.js里面的

可以修改运行文件的路径。。。

electron小例子的更多相关文章

  1. springmvc入门的第一个小例子

    今天我们探讨一下springmvc,由于是初学,所以简单的了解一下 springmvc的流程,后续会持续更新... 由一个小例子来简单的了解一下 springmvc springmvc是spring框 ...

  2. java即时通信小例子

    学习java一段时间了,今天写来一个即时通信的小例子练手在其过程中也学到了一些知识拿出来和大家分享,请路过的各位大神多多赐教... 好了下面讲一下基本的思路: 首先,编写服务器端的程序,简单点说吧就是 ...

  3. Runtime的几个小例子(含Demo)

    一.什么是runtime(也就是所谓的“运行时”,因为是在运行时实现的.)           1.runtime是一套底层的c语言API(包括很多强大实用的c语言类型,c语言函数);  [runti ...

  4. bootstrap 模态 modal 小例子

    bootstrap 模态 modal  小例子 <html> <head> <meta charset="utf-8" /> <title ...

  5. INI配置文件分析小例子

    随手写个解析INI配置字符串的小例子 带测试 #include <iostream> #include <map> #include <string> #inclu ...

  6. JavaScript小例子:复选框全选

    JavaScript小例子:复选框全选 这只是一个小例子,很简单,但是这个功能还是很常用的: 实现后效果如图: JavaScript代码: <script type="text/jav ...

  7. 【zTree】 zTree使用的 小例子

    使用zTree树不是第一次了  但是 还是翻阅着之前做的 对照着 使用起来比较方便  这里就把小例子列出来   总结一下使用步骤 这样方便下次使用起来方便一点 使用zTree树的步骤: 1.首先  在 ...

  8. js小例子(标签页)

    运用js写的一个小例子,实现点击不同的标签出现不同的内容: <!DOCTYPE html> <html> <head> <meta chaset=" ...

  9. sbrk与brk的使用小例子

    sbrk() 和 brk() - Unix的系统函数   sbrk()和brk() 系统的底层会维护一个位置,通过位置的移动完成内存的分配和回收.映射内存时 以一个内存页作为基本单位.   void* ...

随机推荐

  1. POJ 3320 Jessica's Reading Problem 尺取法

    Description Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The fina ...

  2. BZOJ 2561 最小生成树(最大流)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2561 题意:给定一个边带正权的连通无向图G= (V,E),其中N=|V|,M=|E|,N ...

  3. oracle, create table, insufficient privileges

    SQL> exec pro_gz_day_report;          ORA-01031: insufficient privileges          ORA-06512: at & ...

  4. Codeforces Round #281 (Div. 2) C. Vasya and Basketball 二分

    C. Vasya and Basketball time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  5. 移动端图表插件jChart.js的修改

    bug1: 折线图,设置datasetGesture : true时,Y轴的刻度值居然会变.会变也就算了,居然没地方设置不能变. bug2: 折线图,设置tap.point事件,和datasetGes ...

  6. 详解 ASP.NET异步

    在前文中,介绍了.NET下的多种异步的形式,在WEB程序中,天生就是多线程的,因此使用异步应该更为谨慎.本文将着重展开ASP.NET中的异步. [注意]本文中提到的异步指的是服务器端异步,而非客户端异 ...

  7. iOS - UIActionSheet

    前言 NS_CLASS_DEPRECATED_IOS(2_0, 8_3, "UIActionSheet is deprecated. Use UIAlertController with a ...

  8. SAP接口编程 之 JCo3.0系列(03) : Table参数

    Table参数作为export parameter BAPI_COMPANYCODE_GETDETAIL是一个适合演示的函数,没有import paramter参数,调用后COMPANYCODE_GE ...

  9. 【转】C/C++ struct/class/union内存对齐

    原文链接:http://www.cnblogs.com/Miranda-lym/p/5197805.html struct/class/union内存对齐原则有四个: 1).数据成员对齐规则:结构(s ...

  10. mysql: 1045 access denied for user 'root'@'localhost' using password yes

    原因是:root的密码错误了. 解决思路:关闭mysql服务,重新启动mysql服务,启动mysql的时候,指定不需要校验密码.然后登陆mysql,修改密码,退出.再重新启动mysql服务. 1.关闭 ...