当前位置: 首页 > news >正文

electron中拖动文件到桌面案例

效果图

main.js

constpath=require('node:path');const{app,BrowserWindow,ipcMain,nativeImage}=require('electron');constSAFE_FILES=['file1.txt'];functioncreateWindow(){constwin=newBrowserWindow({width:800,height:600,title:'File Drag Demo',webPreferences:{preload:path.join(__dirname,'preload.js'),contextIsolation:true,nodeIntegration:false,},});win.loadFile('index.html');}app.whenReady().then(()=>{ipcMain.on('get-draggable-files-sync',(event)=>{event.returnValue=SAFE_FILES.map((name)=>({name,path:path.resolve(__dirname,name),}));});ipcMain.on('start-file-drag-sync',(event,filePath)=>{constallowedPaths=newSet(SAFE_FILES.map((name)=>path.resolve(__dirname,name)));consttargetPath=path.resolve(filePath);event.returnValue=false;if(!allowedPaths.has(targetPath)){return;}consticonPath=path.join(__dirname,'drag-icon.png');constdragIcon=nativeImage.createFromPath(iconPath);if(dragIcon.isEmpty()){console.error(`Failed to decode drag icon:${iconPath}`);return;}try{event.sender.startDrag({file:targetPath,icon:dragIcon,});event.returnValue=true;}catch(error){console.error('startDrag failed:',error);}});createWindow();app.on('activate',()=>{if(BrowserWindow.getAllWindows().length===0){createWindow();}});});app.on('window-all-closed',()=>{if(process.platform!=='darwin'){app.quit();}});

preload.js

const{contextBridge,ipcRenderer}=require('electron');contextBridge.exposeInMainWorld('fileDrag',{list(){returnipcRenderer.sendSync('get-draggable-files-sync');},start(filePath){returnipcRenderer.sendSync('start-file-drag-sync',filePath);},});

renderer.js

constfileList=document.querySelector('#fileList');constfiles=window.fileDrag.list();for(constfileEntryoffiles){constitem=document.createElement('div');item.className='file-item';item.draggable=true;item.textContent=fileEntry.name;item.addEventListener('dragstart',(event)=>{constok=window.fileDrag.start(fileEntry.path);if(!ok){event.preventDefault();console.error(`Drag rejected for${fileEntry.name}`);}});fileList.append(item);}

index.html

<!doctypehtml><htmllang="zh-CN"><head><metacharset="UTF-8"/><metaname="viewport"content="width=device-width, initial-scale=1.0"/><title>Electron 文件拖拽 Demo</title><linkrel="stylesheet"href="./styles.css"/></head><body><mainclass="app"><h1>拖文件到桌面</h1><p>按住下面任意文件,直接拖到桌面或文件夹里。</p><sectionclass="file-list"id="fileList"></section></main><scriptsrc="./renderer.js"></script></body></html>

style.css

:root{color-scheme:light dark;font-family:Arial,"Microsoft YaHei",sans-serif;}*{box-sizing:border-box;}body{margin:0;}.app{max-width:420px;margin:48px auto;padding:0 24px;}h1{margin:0 0 8px;font-size:28px;}p{margin:0 0 24px;color:#666;}.file-list{display:grid;gap:12px;}.file-item{padding:16px 18px;border:1px solid #d6d6d6;border-radius:12px;background:#f7f7f7;cursor:grab;user-select:none;}.file-item:active{cursor:grabbing;}@media(prefers-color-scheme:dark){body{background:#202124;color:white;}p{color:#bdbdbd;}.file-item{border-color:#555;background:#2b2c2f;}}@media(prefers-color-scheme:light){body{background:#fff;color:black;}}
http://www.jsqmd.com/news/1151885/

相关文章:

  • Layerdivider:告别手动分层,让AI智能解析你的设计图
  • CAS:142131-37-1,p-NH2-Bn-NOTA,对氨基苄基-NOTA的使用
  • SEO 转 GEO 底层逻辑:哪些能力能复用?哪些必须推翻?
  • 谷歌学术无法访问?英文文献、SCI论文、DOI高效检索方案
  • 使用 Homebrew 安装 CMake 的完整指南
  • csdn保存的仅自己可见的文章无法确保不被用于训练AI
  • DVWA 1.10 命令注入实战:3种难度绕过与防御源码分析(附Payload)
  • 突破单总线瓶颈:任意MCU共用SCL实现多路I2C并行通信实战
  • RuView 新手快速上手指南
  • 国巨PA0402系列汽车级电流检测电阻是汽车电子的“电流之眼“
  • 测试工程师转型 AI 质量保障:从测 Bug 到测“智商“
  • vLLM 与 SGLang 推理框架性能横评:架构、吞吐与延迟深度解析
  • 使用CC Switch管理Codex需切换API地址的原因
  • 3类主流湿敏传感器对比:SHT系列、电阻式与电容式的选型指南
  • 特征选择实战:Sklearn 3大方法(Filter/Wrapper/Embedded)对比与20维数据集应用
  • Codex总是改错项目规则?AGENTS.md这5项必须写清楚
  • LLM应用的后端安全防护:Prompt注入防御与输出内容审核架构
  • 国内做高校AI 教育的公司有哪些?全景解析与实战云的独特路径
  • 欧盟商标申请全攻略,中国仍是最大的申请来源国!
  • OpenClaw专栏第六篇:Docker Compose一键部署与集群扩展
  • Maya保存会报错怎么解决?
  • 另外一个AI说csdn的私有文章不会用于训练AI
  • AD5593R与MSP432P401R的硬件协同设计与信号处理
  • 移动端开发者转型 AI Agent 开发:从 App 到智能助手
  • MQ-4 甲烷传感器 Arduino 实战:0-5V 模拟信号采集与 1000ppm 报警阈值设置
  • LingBot-VLA 2.0:面向跨本体通用机器人控制的从基础模型到真实场景的视觉-语言-动作系统演进
  • PAM8124与PIC18F2525构建高效D类音频放大系统
  • 另一个AI说:gitee私有数据也是不能用于训练AI的
  • RZ9692实训平台JSON配置文件解析:5个关键模块参数配置与避坑指南
  • Java Spring Boot 2.x 路径遍历漏洞修复:从 File 到 FileUtils 的 3 步安全改造