Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Electron 在“中写入文件”;appData";在电子方面。从何处添加导入{app}from";电子;;?_Electron - Fatal编程技术网

Electron 在“中写入文件”;appData";在电子方面。从何处添加导入{app}from";电子;;?

Electron 在“中写入文件”;appData";在电子方面。从何处添加导入{app}from";电子;;?,electron,Electron,我正在做我的第一个电子申请。我正在尝试将文本文件保存到appData文件夹(示例C:\Users\user\appData\Roaming)。我知道我需要从“electron”添加导入{app}有些地方,但我不确定放在哪里 在我的index.js javascript中,我正在编写用户以其形式提交到文本文件的数据库设置。这就是我需要appData目录地址的地方 // Write data to text file var filepath = app.getPath("appData") va

我正在做我的第一个电子申请。我正在尝试将文本文件保存到appData文件夹(示例C:\Users\user\appData\Roaming)。我知道我需要从“electron”添加导入{app}有些地方,但我不确定放在哪里

在我的index.js javascript中,我正在编写用户以其形式提交到文本文件的数据库设置。这就是我需要appData目录地址的地方

// Write data to text file
var filepath = app.getPath("appData") 
var filename = "database_quick_image_forensics.txt"
var inp_data = inp_host + "|" + inp_username + "|" + inp_password + "|" + inp_database_name + "|" + inp_table_prefix;
write_to_file(filepath, filename, inp_data);
我的全部代码如下:

/setup/index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Setup</title>
    <!-- https://electronjs.org/docs/tutorial/security#csp-meta-tag -->

        <!-- CSS -->
            <link rel="stylesheet" type="text/css" href="../_webdesign/dark/dark.css" />
        <!-- // CSS -->


    <!-- jQuery -->
    <script>window.$ = window.jQuery = require('../javascripts/jquery/jquery-3.4.1.js');</script>
    <script src="../javascripts/jquery/jquery-3.4.1.js" charset="utf-8"></script>
    <!-- //jQuery -->

    <!-- jQuery -->
    <script src="./index.js" charset="utf-8"></script>
    <!-- //jQuery -->
</head>
<body>
<div id="main_single_column">
  <h1>Setup</h1>

<!-- Feedback -->
    <div id="feedback_div" class="success">
        <p id="feedback_p">Success</p>
    </div>
<!-- //Feedback -->

<!-- Database connection form -->

      <p>Host:<br />
      <input type="text" name="inp_host" id="inp_host" value="localhost" />
      </p>

      <p>Port:<br />
      <input type="text" name="inpport" id="inp_port" value="" />
      </p>

      <p>Username:<br />
      <input type="text" name="inp_username" id="inp_username" value="root" />
      </p>

      <p>Password:<br />
      <input type="text" name="inp_password" id="inp_password" />
      </p>

      <p>Database name:<br />
      <input type="text" name="inp_database_name" id="inp_database_name" value="quick" />
      </p>

      <p>Table prefix:<br />
      <input type="text" name="inp_table_prefix" id="inp_table_prefix" value="cf_" />
      </p>

      <p>
      <button id="form_connect_to_database_submit">Connect to database</button>
      </p>

<!-- //Database connection form -->
</div>



</body>
</html>
/main.js

const fs = require('fs');

// Action = On submit
$(document).ready(function(){

    $("#form_connect_to_database_submit").click( function() {
        // Feedback
        $('#feedback_div').show();
        $('#feedback_div').removeClass("success");
        $('#feedback_div').addClass("info");
        $('#feedback_p').text("Connecting!")

        // get all the inputs
        var inp_host = $("#inp_host"). val();
        var inp_username = $("#inp_username"). val();
        var inp_password = $("#inp_password"). val();
        var inp_database_name = $("#inp_database_name"). val();
        var inp_table_prefix = $("#inp_table_prefix"). val();

        // Test connection
        var connection_result = connect_to_database(inp_host, inp_username, inp_password, inp_database_name, inp_table_prefix);
        if(connection_result != "connection_ok"){
            // Connection Failed
            $('#feedback_div').removeClass("info");
            $('#feedback_div').addClass("error");
            $('#feedback_p').text(connection_result)
        }
        else{
            // Connection OK
            $('#feedback_div').removeClass("info");
            $('#feedback_div').addClass("success");
            $('#feedback_p').text("Connected")

            // Write data to text file
            var filepath = app.getPath("appData") 
            var filename = "database_quick_image_forensics.txt"
            var inp_data = inp_host + "|" + inp_username + "|" + inp_password + "|" + inp_database_name + "|" + inp_table_prefix;
            $('#feedback_p').text("Connected " + filepath)
            write_to_file(filepath, filename, inp_data);

            // Feedback
            $('#feedback_div').removeClass("info");
            $('#feedback_div').addClass("success");
            $('#feedback_p').text("Connected to")
        }




    });
    $('#inp_host').focus();
});


// Function connect to database
function connect_to_database(inp_host, inp_username, inp_password, inp_database_name, inp_table_prefix){
    var mysql = require('mysql');

    // Add the credentials to access your database
    var connection = mysql.createConnection({
        host     : inp_host,
        user     : inp_username,
        password : null, // or the original password : 'apaswword'
        database : inp_database_name
    });

    // connect to mysql
    connection.connect(function(err) {
        // in case of error
        if(err){
            console.log(err.code);
            console.log(err.fatal);
            return err.code;
        }
    });


    // Perform a query
    $query = 'SELECT * FROM `cf_admin_liquidbase` LIMIT 10';
    connection.query($query, function(err, rows, fields) {
        if(err){
            console.log("An error ocurred performing the query.");
            console.log(err);
            return;
        }
        console.log("Query succesfully executed", rows);
    });

    return "connection_ok";
} // connect_to_database




// Function write setup
function write_to_file(filepath, filename, inp_data){


    var fullpath = filepath + "\\" + filename;
    fs.writeFile(fullpath, inp_data, (err) => {
        // throws an error, you could also catch it here
        if (err) throw err;
        // success case, the file was saved
        console.log('Lyric saved!');
    });
} // write_to_file
const { app, BrowserWindow } = require('electron')


// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let win


function createWindow () {
  // Create the browser window.
  win = new BrowserWindow({
          width: 800,
          height: 600,
          webPreferences: {
          nodeIntegration: true
          }
  })

  // and load the index.html of the app.
  win.loadFile('index.html')

  // Open the DevTools.
  // win.webContents.openDevTools()

  // Emitted when the window is closed.
  win.on('closed', () => {
          // Dereference the window object, usually you would store windows
          // in an array if your app supports multi windows, this is the time
          // when you should delete the corresponding element.
          win = null
  })
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)

// Quit when all windows are closed.
app.on('window-all-closed', () => {
  // On macOS it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') {
          app.quit()
  }
})

app.on('activate', () => {
  // On macOS it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (win === null) {
          createWindow()
  }
})

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
我知道我需要从“electron”添加import{app};有些地方,但我 我不知道放在哪里

根据我的经验,该模块总是在
main
过程中导入,因此您可以控制应用程序的生命周期。但是,如果您想在
渲染器
过程中使用一些
应用程序
模块功能,可以通过
远程
模块将其导入其中(如此问题的公认答案所示:)

main
renderer
过程是
Electron
中的关键概念,因此我建议阅读这些过程。要点是,您有一个
main
进程–它没有可视化表示,它涉及应用程序的生命周期、创建和销毁
renderer
进程(如)、渲染器进程之间的通信等–您可以根据需要拥有任意多个
renderer
进程

因此,如果您想读取和写入文件,可以在如上所示的
渲染器
过程中执行,也可以在
main
过程中执行。在后一种情况下,如果
渲染器
进程想要保存文件,它可以通过
main
进程发送消息,发送要保存的数据


使用哪种方式是一种体系结构选择。

在主进程中获取应用程序路径。然后在main.js中使用此代码

switch(process.platform) {
    case 'darwin': {
      return path.join(process.env.HOME, 'Library', 'Application Support', ...);
    }
    case 'win32': {
      return path.join(process.env.APPDATA, ...);
    }
    case 'linux': {
      return path.join(process.env.HOME, ...);
    }
}
  const { ipcMain } = require('electron')
  const appPath = () => {
    switch(process.platform) {
      case 'darwin': {
        return path.join(process.env.HOME, 'Library', 'Application Support');
      }
      case 'win32': {
        return process.env.APPDATA;
      }
      case 'linux': {
        return process.env.HOME;
      }
    }
  }

  const writeToFile = (fileName, inData) => {
      const fullPath = path.join(appPath(), "\\", fileName);
      fs.writeFile(fullPath, inData, (err) => {
        // throws an error, you could also catch it here
        if (err) throw err;
        // success case, the file was saved
        console.log('Lyric saved!');
    });
  } // write_to_file

  ipcMain.on('WRITE_TEXT', async (event, arg) => {
    writeToFile(arg.fileName, arg.inData)
  });
渲染器中获取路径,然后在渲染器中使用此代码

const remote = require('electron').remote;
const app = remote.app;
console.log(app.getPath('userData'));
但要在渲染器上使用require,请确保节点集成为true

如果我是你,我将在main process获取应用程序路径,并将文件存储在main process中。 因此,在渲染器进程导入许多依赖项不是一个好选择。 渲染器进程主要负责在Chromium浏览器中显示应用程序

因此,要在主进程中执行此操作。用这个

在main.js上

switch(process.platform) {
    case 'darwin': {
      return path.join(process.env.HOME, 'Library', 'Application Support', ...);
    }
    case 'win32': {
      return path.join(process.env.APPDATA, ...);
    }
    case 'linux': {
      return path.join(process.env.HOME, ...);
    }
}
  const { ipcMain } = require('electron')
  const appPath = () => {
    switch(process.platform) {
      case 'darwin': {
        return path.join(process.env.HOME, 'Library', 'Application Support');
      }
      case 'win32': {
        return process.env.APPDATA;
      }
      case 'linux': {
        return process.env.HOME;
      }
    }
  }

  const writeToFile = (fileName, inData) => {
      const fullPath = path.join(appPath(), "\\", fileName);
      fs.writeFile(fullPath, inData, (err) => {
        // throws an error, you could also catch it here
        if (err) throw err;
        // success case, the file was saved
        console.log('Lyric saved!');
    });
  } // write_to_file

  ipcMain.on('WRITE_TEXT', async (event, arg) => {
    writeToFile(arg.fileName, arg.inData)
  });
在渲染器进程中添加此代码

const {ipcRenderer} = require('electron')
ipcRenderer.sendSync('WRITE_TEXT',{fileName, inData})
如您所见,在渲染器进程,这是通过“写入文本”IPC通道将
inp_数据
发送到主进程


还有一件事,看看你的代码。您正在渲染器上连接数据库,这是可能的,但这不是一个正确的选择。请思考,而你有几个渲染器。您也应该将此移到主进程。

请参阅:@NoGrabbing我仍然不知道在哪里添加import@Europa. 请核对我的答案。您可以在渲染器或Main上自由使用此选项。无论如何,请检查我的回答(我试图给出详细答案)。)