admin管理员组文章数量:1394191
I am planning to realize a music player with electron. It will list music from the user’s hard drive.
Is it possible to define a drag’n’drop behavior so that I can drag a html element, e.g. <span>Artist – Title</span>
outside the electron window onto the user’s desktop to copy the actual file?
File is stored here: ~/music/Artist-Title.mp3
.
When drag’n’dropping the span
from my electron window onto the desktop a copy should be made: ~/Desktop/Artist-Title.mp3
.
I am planning to realize a music player with electron. It will list music from the user’s hard drive.
Is it possible to define a drag’n’drop behavior so that I can drag a html element, e.g. <span>Artist – Title</span>
outside the electron window onto the user’s desktop to copy the actual file?
File is stored here: ~/music/Artist-Title.mp3
.
When drag’n’dropping the span
from my electron window onto the desktop a copy should be made: ~/Desktop/Artist-Title.mp3
.
2 Answers
Reset to default 5Working example:
Put into main.js
and copy an icon to be shown while dragging (yourAppDir/img/icon/folder.png)
:
const {ipcMain} = require('electron')
ipcMain.on('ondragstart', (event, filePath) => {
event.sender.startDrag({
file: filePath,
icon: 'img/icon/folder.png'
})
})
Put into renderer.js
, and set the path to the file you want to drop out:
var ipcRenderer = require('electron').ipcRenderer
document.getElementById('drag').ondragstart = (event) => {
event.preventDefault()
ipcRenderer.send('ondragstart', '/Users/tim/dev/test/elektron-drag-out-test/img/icon/folder.png')
}
Create the draggable element inside the body tags of index.html
:
...
<body>
...
<a href="#" id="drag" class="draggable">drag item</a>
</body>
...
I also created a gist for drag in / out.
Another helpful resource: Electron – Add webContents.startDrag(item)
API
I believe this is the API you are looking for.
http://electron.atom.io/docs/api/web-contents/#contentsstartdragitem
remote.getCurrentWebContents().startDrag({
file: 'path/to/file',
icon: 'path/to/file/icon',
});
本文标签: javascriptDrag html element outside electron window to copy local fileStack Overflow
版权声明:本文标题:javascript - Drag html element outside electron window to copy local file? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744683836a2619577.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论