electron node.js 实现文件拖动读取文件

css/styles.css

1 .for_file_drop {
2     width: 100%;
3     height: 100px;
4     background-color: blueviolet;
5 }

index.html

 1 <!DOCTYPE html>
 2 <html>
 3   <head>
 4     <meta charset="UTF-8">
 5
 6     <title>Hello World!</title>
 7     <link rel="stylesheet" href="css/styles.css" />
 8   </head>
 9   <body>
10     <h1>Hello World!</h1>
11     We are using Node.js <span id="node-version"></span>,
12     Chromium <span id="chrome-version"></span>,
13     and Electron <span id="electron-version"></span>.
14
15
16     <div>
17         <h1>Process information</h1>
18         <button onclick="getProcessInfo()">查询系统信息</button>
19         </div>
20
21     <div class="for_file_drop" id="drag_test">
22       <h2>文件拖动到此处</h2>
23       <span></span>
24     </div>
25     <!-- You can also require other files to run in this process -->
26     <script src="./renderer.js"></script>
27
28
29
30   </body>
31 </html>

renderer.js

const fs = require("fs");

const dragWrapper = document.getElementById("drag_test");
dragWrapper.addEventListener("drop",(e)=>{
    e.preventDefault(); //阻止e的默认行为
    const files = e.dataTransfer.files;
    if (files && files.length>=1){
        const path = files[0].path;
        console.log("file:",path);
        const content = fs.readFileSync(path);
        console.log(content.toString());
    }
})
//这个事件也需要屏蔽
dragWrapper.addEventListener("dragover",(e)=>{
    e.preventDefault();
})
function getProcessInfo(){
    console.log("Cpu Usage:", process.getCPUUsage());
    console.log("env",process.env);
    console.log("arc",process.arch);
}

main.js

 1 // Modules to control application life and create native browser window
 2 const {app, BrowserWindow} = require(‘electron‘)
 3 const path = require(‘path‘)
 4
 5 // Keep a global reference of the window object, if you don‘t, the window will
 6 // be closed automatically when the JavaScript object is garbage collected.
 7 let mainWindow
 8
 9 function createWindow () {
10   // Create the browser window.
11   mainWindow = new BrowserWindow({
12     width: 800,
13     height: 600,
14     webPreferences: {
15       preload: path.join(__dirname, ‘preload.js‘),
16       nodeIntegration: true // 注入node模块
17     }
18   })
19
20   // and load the index.html of the app.
21   mainWindow.loadFile(‘index.html‘)
22
23   // Open the DevTools.
24   // mainWindow.webContents.openDevTools()
25
26   // Emitted when the window is closed.
27   mainWindow.on(‘closed‘, function () {
28     // Dereference the window object, usually you would store windows
29     // in an array if your app supports multi windows, this is the time
30     // when you should delete the corresponding element.
31     mainWindow = null
32   })
33 }
34
35 // This method will be called when Electron has finished
36 // initialization and is ready to create browser windows.
37 // Some APIs can only be used after this event occurs.
38 app.on(‘ready‘, createWindow)
39
40 // Quit when all windows are closed.
41 app.on(‘window-all-closed‘, function () {
42   // On macOS it is common for applications and their menu bar
43   // to stay active until the user quits explicitly with Cmd + Q
44   if (process.platform !== ‘darwin‘) app.quit()
45 })
46
47 app.on(‘activate‘, function () {
48   // On macOS it‘s common to re-create a window in the app when the
49   // dock icon is clicked and there are no other windows open.
50   if (mainWindow === null) createWindow()
51 })
52
53 // In this file you can include the rest of your app‘s specific main process
54 // code. You can also put them in separate files and require them here.

其中的第16行是重点,否则使用process时会出现:process is not defined  的错误。

原文地址:https://www.cnblogs.com/abc789/p/12001467.html

时间: 2024-07-28 15:33:17

electron node.js 实现文件拖动读取文件的相关文章

node.js同步及异步读取写入删除文件1

node.js初学中,在文件中同步及异步读取文档的过程: 1.同步读取: var fs=require("fs") //直接读取文档,并将同步返回值,赋值给变量 var data=fs.readFileSync("input.txt"); console.log(data.toString()); 2.异步读取: var fs=require("fs"); //通过回调函数返回获得的data值: fs.readFile("input.t

Nodejs学习笔记(八)--- Node.js + Express 实现上传文件功能(felixge/node-formidable)

目录 前言 formidable简介 创建项目并安装formidable 实现上传功能 运行结果 部分疑惑解析 写在之后 前言 前面讲了一个构建网站的示例,这次在此基础上再说说web的常规功能----文件上传,示例以一个上传图片的功能为例子 上传功能命名用formidable实现,示例很简单! PS:最近比较忙,距上一次更新已经比较久了^_^! formidable简介 nodejs原生实现上传还是比较麻烦,有兴趣的自已去参考一下网上有网友写的代码 这里选择了formidable,也是githu

electron node.js 在 vscode 设置 调试 Debug

在当前工程下,添加一个 .vscode/launch.json 文件 { // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.

通过codehaus来实现json写入文件和读取文件成json对象

原文:通过codehaus来实现json写入文件和读取文件成json对象 代码下载地址:http://www.zuidaima.com/share/1550463256562688.htm pom.xml需要增加如下依赖: <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-lgpl</artifactId> <version>1.

文件_ _android从资源文件中读取文件流并显示的方法

======== 1   android从资源文件中读取文件流并显示的方法. 在android中,假如有的文本文件,比如TXT放在raw下,要直接读取出来,放到屏幕中显示,可以这样: private void doRaw(){ InputStream is = this.getResources().openRawResource(R.raw.ziliao); try{ doRead(is); }catch(IOException e){ e.printStackTrace(); } } pri

【node.js学习】--(3)--读写文件

读写文件 一般读写 新建copyFile.js var fs = require("fs"); function copyFile(src,dest){ varfile = fs.readFileSync(src);//根据文件路劲读取文件 fs.writeFileSync(dest,file);//将内容写入文件 } function main(argv){ copyFile(argv[0],argv[1]); } main(process.argv.slice(2));//接受命令

node.js学习第三天--文件I/O

1.文件I/O fs模块的基本用法 开发中我们经常会有文件I/O的需求,node.js中提供一个名为fs的模块来支持I/O操作,fs模块的文件I/O是对标准POSIX函数的简单封装. 2.writeFile函数的基本用法 文件I/O,写入是必修课之一.fs模块提供writeFile函数,可以异步的将数据写入一个文件, 如果文件已经存在则会被替换.用法如下: 例:fs.writeFile(filename, data, callback) var fs= require("fs"); f

node.js学习笔记之写文件

node.js之写文件 //---------------optfile.js------------------var  fs=  require('fs');module.exports={    writefile:function(path,data){    //异步方式        fs.writeFile(path,  data,  function  (err)  {            if  (err)  {                throw  err;     

node.js学习笔记之读文件

直接读硬盘上aa.txt内容var  http  =  require('http');var  optfile  =  require('./models/optfile');http.createServer(function  (request,  response)  {    response.writeHead(200,  {'Content-Type':  'textml;  charset=utf-8'});    if(request.url!=="/favicon.ico&q