在Electron中讀取本地文件
fs.readFile
讀取用戶本地文件有兩種方式:讀取完整文件和讀取文件的一部分,讀取完整文件是最常用的操作?;居梅ㄈ缦拢?/p>
fs.readFile(filepath, 'utf-8', function (err, data) {
if (err) {
alert("An error occurred reading the file :" + err.message)
return
}
//Display the file contents
console.log("The file content is : " + data)
})
如果想用同步方式讀取文件要使用fs.readFileSync()方法。關于如何讀取文件的一部分可以參考官方文檔。