久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

電子函數讀取本地文件 - FS - 不讀取

Electron function to read a local file - FS - Not reading(電子函數讀取本地文件 - FS - 不讀取)
本文介紹了電子函數讀取本地文件 - FS - 不讀取的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

當我需要讓電子讀取本地文件時,我有一個電子項目.

I have an electron project when I need to get electron to read a local file.

現在我擁有的是這個,它加載并顯示 html 文件的內容.

Right now what I have is this, where it loads and displays the contents of a html file.

我現在只需要它來讀取文件并將其存儲在變量中.

I just need it to read a file and store it on a variable for now.

這是我當前的 main.js:

Here is my current main.js:

 const {app, BrowserWindow } = require('electron');
const path = require('path');
const url = require('url');
var fs = require('fs');

let mainWindow;

function createNewWindow() {
  mainWindow = new BrowserWindow({
    width: 1300,
    height: 1000,
    minWidth: 600,
    minHeight: 400,
    title: 'Test App'
  })
}

function loadInitialUrl() {
  mainWindow.loadURL(url.format({
    pathname: path.join(__dirname, 'index.html'),
    protocol: 'file:',
    slashes: true
  }))
}

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


app.on('ready', function(){
  createNewWindow();
  loadInitialUrl();
  mainWindow.setMenu(null);
  mainWindow.openDevTools();
  fs.readFile('./README.md', 'utf8', function (err,data) {
    if (err) {
      return console.log(err);
    }
    console.log(data);
  });
  mainWindow.on('closed', function() {mainWindow = null;});
});

我該怎么做,因為它沒有在 console.log 中顯示 README.md 文件的內容

How can I do this as it's not showing the contents of the README.md file in the console.log

推薦答案

基本上你需要做以下幾件事.

Basically you need to do the following things.

1.加載所需的依賴項

var remote = require('remote'); // Load remote compnent that contains the dialog dependency
var dialog = remote.require('dialog'); // Load the dialogs component of the OS
var 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 againg
var 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");
});

更多閱讀請訪問 這里 :) 謝謝..

For more read please visit here :) Thanks..

還有一件事要添加..請檢查您的文件路徑是否正確.你可以做類似下面的事情.

One more thing to add..Please check that your path to file is correct. You could do something similar to below.

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

這篇關于電子函數讀取本地文件 - FS - 不讀取的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

discord.js v12: How do I await for messages in a DM channel?(discord.js v12:我如何等待 DM 頻道中的消息?)
how to make my bot mention the person who gave that bot command(如何讓我的機器人提及發出該機器人命令的人)
How to fix Must use import to load ES Module discord.js(如何修復必須使用導入來加載 ES 模塊 discord.js)
How to list all members from a specific server?(如何列出來自特定服務器的所有成員?)
Discord bot: Fix ‘FFMPEG not found’(Discord bot:修復“找不到 FFMPEG)
Welcome message when joining discord Server using discord.js(使用 discord.js 加入 discord 服務器時的歡迎消息)
主站蜘蛛池模板: 欧美久久精品一级黑人c片 91免费在线视频 | 成年免费在线观看 | 99综合| 亚洲综合日韩精品欧美综合区 | 亚洲欧美国产视频 | 色爱区综合 | 久草新视频| 亚洲欧美在线一区 | 国产精品99久久久久久久久久久久 | 成人午夜免费视频 | 久久国产免费看 | 亚洲性视频 | 亚洲国产视频一区 | 欧美在线a | 欧美黄色片 | 一区二区小视频 | 五月激情六月婷婷 | 久久免费精品视频 | 亚洲在线看 | 成人在线精品 | 中文字幕在线一区 | www.久久久久久久久久久 | 久久久久午夜 | 国产清纯白嫩初高生在线播放视频 | 久久99国产精品久久99果冻传媒 | 在线观看中文字幕亚洲 | 午夜精品久久久久久久久久久久久 | 99re视频在线观看 | 国产欧美精品一区二区三区 | 二区三区在线观看 | 欧美激情啪啪 | 91精品国产综合久久久久 | 国产亚洲一区二区精品 | 中文字幕免费 | 老头搡老女人毛片视频在线看 | 在线观看国产视频 | 久久夜夜| 中文在线视频 | 在线一区二区三区 | 国产伦精品一区二区三区高清 | 日本色高清 |