栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

电子功能读取本地文件-FS-不读取

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

电子功能读取本地文件-FS-不读取

基本上,您需要执行以下操作。

1.加载所需的依赖项

var remote = require('remote'); // Load remote compnent that contains the dialog dependencyvar dialog = remote.require('dialog'); // Load the dialogs component of the OSvar fs = require('fs'); // Load the File System to execute our common tasks (CRUD)

2.读取文件内容

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);    });});

3.更新现有文件内容

 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 againgvar 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");});

欲了解更多信息,请访问这里 :)谢谢..

还需要添加一件事。请检查文件路径是否正确。您可以执行以下类似操作。

var path = require('path');var p = path.join(__dirname, '.', 'README.md');


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/448727.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号