javascript - 电子从对话框中读取文件获取类型错误
问题描述
我正在编写一个电子程序,它需要读取一个文件以便我将另一个程序应用到上面。现在我只希望它成功读取并记录文件的内容。
这是我在名为 script.js 的脚本中使用的代码,该脚本包含在 index.html 的脚本标记中
// dialog box
const { dialog } = require('electron').remote;
// file system, needed to read contents of the ecgFile
const {fs} = require('fs');
const dialogOptions = {properties:['openFile']}
// bool for checking if file has been selected
let isFileSelected = false;
let ecgFile; // content of the ecgFile itself
// Buttons
const openFileBtn = document.getElementById('openFileBtn');
// on click event, opens the dialog box and loads content of the file
openFileBtn.onclick = () =>
{
console.log("start")
let filePathArray = dialog.showOpenDialogSync({properties: ['openFile']}) // output the filepath of the file selected
let filePath = filePathArray[0] // set to the first object in the array, which is what is needed
console.log("File Location -- "+ filePath) // print to check its hunky dory
readFile(filePath) // read the file
console.log("End")
};
function readFile(filePath)
{
fs.readFile(filePath,{"encoding": "utf8"},(err,data) =>
{
if (err)
{
console.log("Cannot read file",err);
return;
}
console.log("Content of data: ")
console.log(data);
}
)
}
我能够成功打印我想要使用的文件路径,问题是当我将该文件路径传递给 readFile 函数时
当它运行 readFile 函数时,它总是输出这个错误
Uncaught TypeError: Cannot read property 'readFile' of undefined
at readFile (script.js:55)
at HTMLButtonElement.openFileBtn.onclick (script.js:23)
我一直在寻找没有成功的解决方案。感谢您的时间和帮助
解决方案
推荐阅读
- google-cloud-dataflow - 数据流来处理批处理和流消息的延迟和乱序数据?
- sql - 如何使用 Like 运算符将一个表中的字符串匹配到另一个表中的子字符串
- angular - 使用 BingMap 的应用程序的 Angular 5“ng 测试”失败
- javascript - 无法在 reactjs 中的 componentDidMount 中调用 loaction.state
- powershell - New-PSSession - WinRM 无法处理请求
- qt - 在 Visual Studio 中测试 Qt 应用程序 - 在测试文件中包含项目文件时,Qt 文件给出“无法打开源文件”错误
- javascript - 使用 jQuery/JavaScript 启用和禁用选项卡
- r - r 根据列表复制到 wd 之外
- azure - Select-AzureRmSubscription :请提供有效的租户或有效的订阅
- android - 有谁知道如何从 android 平台中的 facebook sdk 获取名字、姓氏、电子邮件、身份证、生日、性别、家乡、年龄等?