美文网首页
读取文件demo

读取文件demo

作者: coffee1949 | 来源:发表于2019-06-29 15:57 被阅读0次

package.json

{
  "name": "03-readfile",
  "version": "1.0.0",
  "description": "",
  "main": "main.js",    //这里
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "electron ."    // 这里
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

main.js

const path = require('path')
const { app,BrowserWindow } = require('electron')

let mainWindow = null

function createWindow(){
    mainWindow = new BrowserWindow({
        width: 960,
        height: 600,
        webPreferences:{
            nodeIntegration:true
        }
    })

    mainWindow.loadURL(path.join('file://', __dirname, '/index.html'))

    mainWindow.webContents.openDevTools()

    mainWindow.on('closed', () => {
        mainWindow = null
    })
}

app.on('ready',()=>{
    createWindow()
})

app.on('window-all-closed',()=>{
    if(process.platform != 'darwin'){
        app.quit()
    }
})

app.on('active',()=>{
    if(mainWindow === null){
        createWindow()
    }
})

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>electron</title>
</head>
<body>
    <h1>hello electron</h1>
    <button>点我读取</button>
    <hr>
    <textarea style="width: 99%;" rows="20"></textarea>
    <script src="./index.js"></script>
</body>
</html>

index.js

var fs = require('fs')

let btn = document.querySelector('button')
btn.onclick = ()=>{
    fs.readFile('./package.json','utf8',(err,data)=>{
        if(err){
            return err
        }
        document.querySelector('textarea').innerHTML = data
    })
}

相关文章

网友评论

      本文标题:读取文件demo

      本文链接:https://www.haomeiwen.com/subject/ushpcctx.html