electron 读取文件夹内容_如何使用Electron Framework选择,读取,保存,删除或创建文件...
本文概述
为了处理文件(CRUD)的生命周期, 我们将使用对话框和文件系统组件。
对话框模块提供了用于显示本机系统对话框(例如打开文件或警报)的API, 因此Web应用程序可以提供与本机应用程序和Node.js文件系统相同的用户体验。
加载所需的依赖项
我们需要加载以下依赖项, 以执行我们要完成的任务, 例如保存, 打开删除等, 包括”操作系统”对话框。
var remote = require('remote'); // Load remote compnent that contains the dialog dependency
var dialog = remote.require('dialog'); // Load the dialogs component of the OS
var fs = require('fs'); // Load the File System to execute our common tasks (CRUD)
注意
在最新版本的Electron(> = 1)中, 请使用以下代码段访问对话框。
var app = require('electron').remote;
var dialog = app.dialog;
// Or with ECMAScript 6
const {dialog} = require('electron').remote;
建立档案
要创建文件, 我们将使用文件系统, 在这种情况下, 将使用系统的本机保存文件对话框(以检索路径)。你可以使用
let content = "Some text to save into the file";
// You can obviously give a direct path without use the dialog (C:/Program Files/path/myfileexample.txt)
dialog.showSaveDialog((fileName) => {
if (fileName === undefined){
console.log("You didn't save the file");
return;
}
// fileName is a string that contains the path and filename created in the save file dialog.
fs.writeFile(fileName, content, (err) => {
if(err){
alert("An error ocurred creating the file "+ err.message)
}
alert("The file has been succesfully saved");
});
});
读取文件内容
要读取文件路径, 我们还将使用文件系统和本机读取文件对话框。
dialog.showOpenDialog((fileNames) => {
// fileNames is an array that contains all the selected
if(fileNames === undefined){
console.log("No file selected");
return;
}
fs.readFile(filepath, 'utf-8', (err, data) => {
if(err){
alert("An error ocurred reading the file :" + err.message);
return;
}
// Change how to handle the file content
console.log("The file content is : " + data);
});
});
// Note that the previous example will handle only 1 file, if you want that the dialog accepts multiple files, then change the settings:
// And obviously , loop through the fileNames and read every file manually
dialog.showOpenDialog({
properties: [
'openFile', 'multiSelections', (fileNames) => {
console.log(fileNames);
}
]
});
更新现有文件内容
要更新现有文件, 我们仅需要使用以下代码的文件路径(如果需要再次使用filedialog或使用先前保存的文件路径, 则可以检索该文件路径):
var filepath = "C:/Previous-filepath/existinfile.txt";// you need to save the filepath when you open the file to update without use the filechooser dialog againg
var content = "This is the new content of the file";
fs.writeFile(filepath, content, (err) => {
if (err) {
alert("An error ocurred updating the file" + err.message);
console.log(err);
return;
}
alert("The file has been succesfully saved");
});
删除档案
要删除文件, 你只需要文件的路径并使用exist和unlink方法。 exist方法检查文件是否存在, 然后使用unlink方法删除该文件。
var filepath = "C:/Path-toFile/file.txt";// Previously saved path somewhere
if (fs.existsSync(filepath)) {
fs.unlink(filepath, (err) => {
if (err) {
alert("An error ocurred updating the file" + err.message);
console.log(err);
return;
}
console.log("File succesfully deleted");
});
} else {
alert("This file doesn't exist, cannot delete");
}
很简单不是吗?
选择一个文件夹
你可以使用对话框选择一个文件夹, 以检索文件夹路径:
dialog.showOpenDialog({
title:"Select a folder", properties: ["openDirectory"]
}, (folderPaths) => {
// folderPaths is an array that contains all the selected paths
if(fileNames === undefined){
console.log("No destination folder selected");
return;
}else{
console.log(folderPaths);
}
});
完整的工作示例
以下html文件可用于在你的项目中进行测试, 以了解文件系统的工作方式。该代码将生成一个简单的Crud UI。
Our Code World
The file content will be the same as the editor.
var remote = require('remote');
var dialog = remote.require('dialog');
var fs = require('fs');
document.getElementById('select-file').addEventListener('click', function(){
dialog.showOpenDialog(function (fileNames) {
if(fileNames === undefined){
console.log("No file selected");
}else{
document.getElementById("actual-file").value = fileNames[0];
readFile(fileNames[0]);
}
});
}, false);
document.getElementById('save-changes').addEventListener('click', function(){
var actualFilePath = document.getElementById("actual-file").value;
if(actualFilePath){
saveChanges(actualFilePath, document.getElementById('content-editor').value);
}else{
alert("Please select a file first");
}
}, false);
document.getElementById('delete-file').addEventListener('click', function(){
var actualFilePath = document.getElementById("actual-file").value;
if(actualFilePath){
deleteFile(actualFilePath);
document.getElementById("actual-file").value = "";
document.getElementById("content-editor").value = "";
}else{
alert("Please select a file first");
}
}, false);
document.getElementById('create-new-file').addEventListener('click', function(){
var content = document.getElementById("content-editor").value;
dialog.showSaveDialog(function (fileName) {
if (fileName === undefined){
console.log("You didn't save the file");
return;
}
fs.writeFile(fileName, content, function (err) {
if(err){
alert("An error ocurred creating the file "+ err.message)
}
alert("The file has been succesfully saved");
});
});
}, false);
function readFile(filepath) {
fs.readFile(filepath, 'utf-8', function (err, data) {
if(err){
alert("An error ocurred reading the file :" + err.message);
return;
}
document.getElementById("content-editor").value = data;
});
}
function deleteFile(filepath){
fs.exists(filepath, function(exists) {
if(exists) {
// File exists deletings
fs.unlink(filepath, function(err){
if(err){
alert("An error ocurred updating the file"+ err.message);
console.log(err);
return;
}
});
} else {
alert("This file doesn't exist, cannot delete");
}
});
}
function saveChanges(filepath, content){
fs.writeFile(filepath, content, function (err) {
if(err){
alert("An error ocurred updating the file"+ err.message);
console.log(err);
return;
}
alert("The file has been succesfully saved");
});
}
玩得开心 !
electron 读取文件夹内容_如何使用Electron Framework选择,读取,保存,删除或创建文件...相关推荐
- python opencv 从Intel Realsense D435 视频流中读取并显示帧,按下空格将图像保存到指定文件夹,按下回车自动以一定时间间隔保存图像至指定文件夹
参考文章1:opencv之读入一幅图像,显示图像以及如何保存一副图像,基础操作 参考文章2:python-OpenCV2中 cv2.VideoCapture(),read(),waitKey()的使用 ...
- sudo修改文件夹名字_修改mac os帐户的短名称和个人文件夹
根据"系统偏好设置"的"用户"面板中的定义,Mac OS X 中的每个用户都拥有一个全"名称"和一个"短名称".短名称最 ...
- WINDOWS 文件夹内容
WINDOWS 文件夹内容 WINDOWS 文件夹内容的介绍,看了你就明白了. ├-WINDOWS │ ├-system32(存放Windows的系统文件和硬件驱动程序) │ │ ├-config(用 ...
- 多个html文件内容合并,Bat批量将多个文件夹内容合并一个文件夹
方法一 把各文件夹内所有文件内并到自动建的一个合并文件夹内,如有同名文件在主名后附容_序号. 将以下内容保存到新建的txt文件中,重命名txt文件后缀用txt改为bat,双击运行.@echo off ...
- svn取消文件夹图标_如何去掉svn文件夹上面的符号
如何去掉svn文件夹上面的符号 如何去掉svn文件夹上面的符号 第一步:建立一个名字叫做remove-svn-folders.reg的文本(先建立txt文件,然后粘贴内容后再修改文件名字),记得后缀要 ...
- python 复制文件夹内容 并结构一致_Python-移动和覆盖文件和文件夹
Python-移动和覆盖文件和文件夹 我有一个目录" Dst Directory",其中包含文件和文件夹,而我有" src Directory",其中也包含文件 ...
- 的文件夹结构_小白指南:WordPress文件及目录结构解析
想学习关于WordPress文件和目录结构的知识吗?WordPress核心软件.主题.插件以及用户上传的文件都存储在网站上.在这篇小白指南中,我们将解析WordPress文件和目录结构. 为什么需要学 ...
- geetest文件夹什么意思_手机文件夹是英文不敢删?只要找出这5个文件夹,能腾出大量内存...
很多朋友都知道,删除手机文件夹是有效清理内存的方法,但是大部分手机里的文件夹都是英文名字,大家都不知道是什么意思,所以也不敢"轻举妄动".没关系,今天小科就带你了解了解,哪些文件夹 ...
- 存储在icloud云盘文件夹顶层_重大问题!icloud云盘自动重命名且丢失文件… - Apple 社区...
情景:家中是mac,公司是windows,为了在家使用mac工作方便而将公司文件储存在icloud云盘中共享. 配置:公司windows 10电脑上安装最新icloud云盘客户端,并将文件保存在icl ...
最新文章
- html5+css3基础教程收集
- 一款java代码生成器(我受够了加班),走起!
- 个性化资源管理器软件Q-dir_6.36(附安装包)
- 数据库为什么不适合搜索引擎的底层存储?
- 开源的DevOps开发工具箱
- 手把手教你创建容器服务Kubernetes集群
- rageframe2 数据库配置_RF 微商城 一款基于 RageFrame2 的免费开源的基础销售功能的微商城...
- 产品经理欲哭无泪的瞬间2(太真实了)
- lambda 复制数组
- 【转】常用的正则表达式
- I/O端口及其寻址方式
- Android ExpandableListView示例教程
- 精讲贪吃蛇(c语言篇)(代码可直接取)
- deepnode软件下载地址_office2010 软件下载地址
- 用rollup打包vue组件库
- ssrf dict MySQL_SSRF之利用dict和gopher吊打Redis
- HDS存储管理工具命令行汇总
- 光辉岁月--beyond 献给麦德拉
- PAKE: Password-authenticated key agreement
- Raspberry Pi使用教程
热门文章
- Java 数组实现堆栈操作
- codevs1219 骑士遍历(棋盘DP)
- atitit.表单验证 的dsl 本质跟 easyui ligerui比较
- 读取XML文件的节点内的内容
- java入门5-asp.net关注
- Lachesis Shield 设计上的抉择
- Java开发人员的十大戒律
- css --- 选择器
- 03 渲染元素ReactDOM.render
- Istio流量管理实践之(5): 使用cert-manager部署Istio自定义入口网关及进行证书管理...