1. // Modules to control application life and create native browser window
    2. const {app, BrowserWindow} = require('electron')
    3. const path = require('path')
    4. function createWindow () {
    5. // Create the browser window.
    6. const mainWindow = new BrowserWindow({
    7. width: 800,
    8. height: 600,
    9. webPreferences: {
    10. preload: path.join(__dirname, 'preload.js')
    11. }
    12. })
    13. // and load the index.html of the app.
    14. mainWindow.loadFile('index.html')
    15. // Open the DevTools.
    16. // mainWindow.webContents.openDevTools()
    17. }
    18. // This method will be called when Electron has finished
    19. // initialization and is ready to create browser windows.
    20. // Some APIs can only be used after this event occurs.
    21. app.whenReady().then(() => {
    22. createWindow()
    23. app.on('activate', function () {
    24. // On macOS it's common to re-create a window in the app when the
    25. // dock icon is clicked and there are no other windows open.
    26. if (BrowserWindow.getAllWindows().length === 0) createWindow()
    27. })
    28. })
    29. // Quit when all windows are closed, except on macOS. There, it's common
    30. // for applications and their menu bar to stay active until the user quits
    31. // explicitly with Cmd + Q.
    32. app.on('window-all-closed', function () {
    33. if (process.platform !== 'darwin') app.quit()
    34. })
    35. // In this file you can include the rest of your app's specific main process
    36. // code. You can also put them in separate files and require them here.
    1. <!DOCTYPE html>
    2. <html>
    3. <head>
    4. <meta charset="UTF-8">
    5. <!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
    6. <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'">
    7. <link href="./styles.css" rel="stylesheet">
    8. <title>Hello World!</title>
    9. <style>
    10. .for_file_drag {
    11. width: 100%;
    12. height: 400px;
    13. background: pink;
    14. }
    15. </style>
    16. </head>
    17. <body>
    18. <!-- <h1>Hello World!</h1>
    19. We are using Node.js <span id="node-version"></span>,
    20. Chromium <span id="chrome-version"></span>,
    21. and Electron <span id="electron-version"></span>. -->
    22. <!-- You can also require other files to run in this process -->
    23. <div class="for_file_drag" id="drag_test">
    24. <h2>File对象</h2>
    25. <span>往这里拖文件</span>
    26. </div>
    27. <script src="./renderer.js"></script>
    28. </body>
    29. </html>
    1. // This file is required by the index.html file and will
    2. // be executed in the renderer process for that window.
    3. // No Node.js APIs are available in this process because
    4. // `nodeIntegration` is turned off. Use `preload.js` to
    5. // selectively enable features needed in the rendering
    6. // process.
    1. // All of the Node.js APIs are available in the preload process.
    2. // It has the same sandbox as a Chrome extension.
    3. // File对象 实例
    4. const fs = require('fs');
    5. window.addEventListener('DOMContentLoaded', () => {
    6. const replaceText = (selector, text) => {
    7. const element = document.getElementById(selector)
    8. if (element) element.innerText = text
    9. }
    10. for (const type of ['chrome', 'node', 'electron']) {
    11. replaceText(`${type}-version`, process.versions[type])
    12. }
    13. //获取div对象
    14. const dragWrapper = document.getElementById("drag_test");
    15. //添加拖拽事件监听器
    16. dragWrapper.addEventListener("drop", (e) => {
    17. //阻止默认行为
    18. e.preventDefault();
    19. //获取文件列表
    20. const files = e.dataTransfer.files;
    21. if (files && files.length > 0) {
    22. //获取文件路径
    23. const path = files[0].path;
    24. console.log('path:', path);
    25. //读取文件内容
    26. const content = fs.readFileSync(path);
    27. console.log(content.toString());
    28. }
    29. })
    30. //阻止拖拽结束事件默认行为
    31. dragWrapper.addEventListener("dragover", (e) => {
    32. e.preventDefault();
    33. })
    34. })
    1. /* styles.css */
    2. /* Add styles here to customize the appearance of your app */