[英]Electron read file from dialog box getting type error
我正在編寫一個 electron 程序,該程序需要讀取一個文件以供我應用另一個程序。 現在我只希望它成功讀取並記錄文件的內容。
這是我在名為 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 function
運行readFile function時,總是輸出這個錯誤
Uncaught TypeError: Cannot read property 'readFile' of undefined
at readFile (script.js:55)
at HTMLButtonElement.openFileBtn.onclick (script.js:23)
我一直在尋找沒有成功的解決方案。 感謝您的時間和幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.