A Brief Look at Node’s FS Module
读写文件操作调用了Node的核心模块:
fs
(File System)。这个模块的每个方法都提供了同步和异步两种版本,通常选择异步方式编写代码。异步方式就能防止打断用户交互操作,因为代码的执行不会被阻塞。
fs
模块基本的操作如下:
* 打开或创建文件
* 获取文件状态和信息
* 写文件
* 读文件
* 关闭文件
* 删除文件
使用
FS
模块之前首先要导入它:
const fs = require('fs')
通常情况下,我们不需要手动打开和关闭工作文件,标准读写命令就可以自动化处理。但如果想操作流文件或访问文件的特定块,你就需要使用如下方式正确处理了:
fs.open(path, flags[, mode], callback)
各个参数的解释如下表:
FS
模块的
flag
参数支持多种文件访问方式,如下表所示:
获取文件信息
通过
fs.stats
方法可以获取文件信息。这个方法可以确定是文件还是一个目录。如下代码段进入某个目录,然后输出了文件信息:
fs.stat(filePath, function (err, stats) {
if (err) {
return console.error(err)
console.log(stats)
console.log("Got file info successfully!")
console.log("isFile ? " + stats.isFile())
console.log("isDirectory ? " + stats.isDirectory())
输出文件信息:
fs.Stats
atime: Mon Mar 13 2017 15:13:31 GMT-0700 (PDT)
birthtime: Mon Mar 13 2017 15:09:16 GMT-0700 (PDT)
blksize: 4096blocks: 8
ctime: Mon Mar 13 2017 15:13:12 GMT-0700 (PDT)
dev: 16777220
gid: 1859252656ino: 30007351
mode: 33188
mtime: Mon Mar 13 2017 15:13:12 GMT-0700 (PDT)
nlink: 1
rdev: 0
size: 603
uid: 224974590
Got file info successfully!
isFile ? true
isDirectory ? false
fs.writeFile
方法可以对文件进行写操作:
fs.writeFile(file, data[, options], callback)
参数解释如下:
基本用法如下所示:
fs.writeFile(fileName, content, function (err) {
if (err) {
console.log("An error occurred creating the file " + err.message)
} else {
console.log("The file has been successfully saved")
fs.readFile
读取用户本地文件有两种方式:读取完整文件和读取文件的一部分,读取完整文件是最常用的操作。基本用法如下:
fs.readFile(filepath, 'utf-8', function (err, data) {
if (err) {
alert("An error occurred reading the file :" + err.message)
return
console.log("The file content is : " + data)
如果想用同步方式读取文件要使用fs.readFileSync()
方法。关于如何读取文件的一部分可以参考官方文档。
fs.unlink()
方法能够删除用户电脑上的文件。由于这个操作基于标准POSIX方法(Portable Operating System Interface of UNIX,用于操作文件和目录的标准命令集),所以删除方法叫做unlink。可以使用fs.existsSync()
测试文件是否还存在:
if (fs.existsSync(filePath)) {
fs.unlink(filepath, function (err) {
if (err) {
console.log("An error ocurred updating the file" + err.message)
return
console.log("File succesfully deleted")
监测文件更新
另一个有用的方法是fs.watch()
,用以监测文件更新:
fs.watch(fileName, {
persistent: true
}, function(event, filename) {
console.log(event + " event occurred on " + filename)
上面的例子都是对文件的操作,fs
模块同样支持目录操作。
新建目录使用fs.mkdir()
方法:
fs.mkdir(myDir, function(err){
if (err) {
console.log('mkdir err:'+err)
console.log('New Directory Created')
读取目录信息
使用fs.readdir()
或同步的fs.readdirSync()
方法能够读取目录的信息。调用的结果是一个当前目录下文件和子目录的字符串数组。例如在electron-quick-start
目录下调用该方法:
fs.readdir('./', function(err, files){
if (err) {
console.log(‘readdir err:'+err)
return
console.log(files)
得到的结果为:
[".git", ".gitignore", "LICENSE.md", "README.md", "index.html", "main.js", "node_modules", "package.json", "renderer.js"]
使用fs.rmdir()
或同步的fs.rmdirSync()
方法能够删除目录,并且不会返回信息:
fs.rmdir(myDir, function(err){
if (err) {
console.log('rmdir err:'+err)
return
console.log('deleted the directory')
更多关于Nodefs
模块的用法请参考官方文档。
A Brief Look at Node’s FS Module读写文件操作调用了Node的核心模块:fs(File System)。这个模块的每个方法都提供了同步和异步两种版本,通常选择异步方式编写代码。异步方式就能防止打断用户交互操作,因为代码的执行不会被阻塞。fs模块基本的操作如下: * 打开或创建文件 * 获取文件状态和信息 * 写文件 * 读文件 * 关闭文件 * 删除文...
这可能是您想要的应用程序。
const { app , BrowserWindow } = require ( 'electron' ) ;
require ( 'electron-file-downloader' ) ( ) ;
let win ;
app . on ( 'ready' , ( ) => {
win = new BrowserWindow ( ) ;
文档文件 例如 markdown、doc、excel 等这类开发文档文件
C++ 源代码文件,如 cpp、cc、h、hpp 等
objecC 源代码文件,mm、m、swift 文件等
mac端才能使用的二进制文件如 .dylib、.framework、.a等文件
可能没有用的压缩文件 tar、7z、zip文件(可选)
# 删除一些无关平台文件
/usr/bin/find $c
Electron教程(5)读取本地文件内容, icpMain icpRenderer 之间的交互
读取本地文件的原理: electron 的主进程里可以运行所有node 的功能,包含通过 os 读取系统信息,通过 fs 读取文件和目录。
那么如何实现文件的读取和展示呢? 因为渲染进程是无法直接跟系统直接交互的,所以分两个部分:
主进程负责读取文件
主进程把读到的文件内容 -> 传递给渲染进程,再由渲染进程负责展示
知道如何使用这个,几乎所有其它的 node 相关的功能也都知道该如何使用了
打印出来是空对象。
经过查阅electron issue,
原因:webpack在打包时截胡了require(‘fs’),使其去找node_modules下的fs,其实是不存的。
解决方案如下:
二、查看electron自身node版本
在electron进程中,有全局对象process
控制台打印。process.versions.node
三、fs....
webPreferences: {
// 是否启用Node integration
nodeIntegration: true, // Electron 5.0.0 版本之后它将被默认false
// 是否在独立 JavaScript 环境中运行 Electron API和指定的preload 脚本.默认为 true
contextIsolation: false, // Electron 12 版本之后它将被默认tr
electron写应用时,会遇到自动上传的需求。但是H5中只能通过input(type=file)来手动上传,JS又没有读取文件的权限,此时,我们可以借助node模块完成需求。
1. node读取文件流
fs模块读取本地文件,在主、进程通信时候,将要读取的文件路径filePath传给node。
主程main.js
fs.readFile(filePath,(err,data)=>{
2. electron是由俩种进程组成,包括主进程和0个或n个渲染进程。
2.1 主进程:承担应用的生命周期(包括启动,退出,准备,正在切换到后台,正在切换到前台等,还负责与原生操作系统API通信)。
2.2 渲染进程:做web页面的ui,渲染进程之间独立在各自的单线程,渲染进程之间相互隔离,不能直接访问操作系统,需要通信到主线程,在通过主线程操作访问操作心态。一个BrowserWindow实例即为一个渲染进程。
3. electron整.
主要使用技术为nodejs的fs模块,以及electron的dialog
使用dialog.showSaveDialog获取导出文件的路径,然后调用fs.writeFileSync同步写入文件即可。
var filters = [
name: filename,
ex...
const fs = require("fs") as typeof import("fs");
const path = require("path") as typeof import("path");
// code here ...