2018-02-20 00:20:21 +00:00
|
|
|
# Native File Drag & Drop
|
|
|
|
|
2020-10-16 02:14:16 +00:00
|
|
|
## Overview
|
|
|
|
|
2018-02-20 00:20:21 +00:00
|
|
|
Certain kinds of applications that manipulate files might want to support
|
|
|
|
the operating system's native file drag & drop feature. Dragging files into
|
|
|
|
web content is common and supported by many websites. Electron additionally
|
|
|
|
supports dragging files and content out from web content into the operating
|
|
|
|
system's world.
|
|
|
|
|
2020-10-16 02:14:16 +00:00
|
|
|
To implement this feature in your app, you need to call the
|
|
|
|
[`webContents.startDrag(item)`](../api/web-contents.md#contentsstartdragitem)
|
2018-02-20 00:20:21 +00:00
|
|
|
API in response to the `ondragstart` event.
|
|
|
|
|
2020-10-16 02:14:16 +00:00
|
|
|
## Example
|
|
|
|
|
2021-05-19 00:55:24 +00:00
|
|
|
An example demonstrating how you can create a file on the fly to be dragged out of the window.
|
|
|
|
|
|
|
|
### Preload.js
|
|
|
|
|
2023-01-16 09:22:49 +00:00
|
|
|
In `preload.js` use the [`contextBridge`][] to inject a method `window.electron.startDrag(...)` that will send an IPC message to the main process.
|
2021-05-19 00:55:24 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
const { contextBridge, ipcRenderer } = require('electron')
|
|
|
|
|
|
|
|
contextBridge.exposeInMainWorld('electron', {
|
|
|
|
startDrag: (fileName) => {
|
|
|
|
ipcRenderer.send('ondragstart', path.join(process.cwd(), fileName))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
```
|
|
|
|
|
|
|
|
### Index.html
|
|
|
|
|
|
|
|
Add a draggable element to `index.html`, and reference your renderer script:
|
2018-02-20 00:20:21 +00:00
|
|
|
|
|
|
|
```html
|
2021-05-19 00:55:24 +00:00
|
|
|
<div style="border:2px solid black;border-radius:3px;padding:5px;display:inline-block" draggable="true" id="drag">Drag me</div>
|
2020-10-16 02:14:16 +00:00
|
|
|
<script src="renderer.js"></script>
|
2018-02-20 00:20:21 +00:00
|
|
|
```
|
|
|
|
|
2021-05-19 00:55:24 +00:00
|
|
|
### Renderer.js
|
2020-10-16 02:14:16 +00:00
|
|
|
|
2023-01-16 09:22:49 +00:00
|
|
|
In `renderer.js` set up the renderer process to handle drag events by calling the method you added via the [`contextBridge`][] above.
|
2020-10-16 02:14:16 +00:00
|
|
|
|
2021-05-19 00:55:24 +00:00
|
|
|
```javascript
|
2020-10-16 02:14:16 +00:00
|
|
|
document.getElementById('drag').ondragstart = (event) => {
|
|
|
|
event.preventDefault()
|
2021-05-19 00:55:24 +00:00
|
|
|
window.electron.startDrag('drag-and-drop.md')
|
2020-10-16 02:14:16 +00:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-05-19 00:55:24 +00:00
|
|
|
### Main.js
|
2020-10-16 02:14:16 +00:00
|
|
|
|
2021-05-19 00:55:24 +00:00
|
|
|
In the Main process (`main.js` file), expand the received event with a path to the file that is
|
2020-10-16 02:14:16 +00:00
|
|
|
being dragged and an icon:
|
2018-02-20 00:20:21 +00:00
|
|
|
|
2020-11-30 07:48:39 +00:00
|
|
|
```javascript fiddle='docs/fiddles/features/drag-and-drop'
|
2018-02-20 00:20:21 +00:00
|
|
|
const { ipcMain } = require('electron')
|
|
|
|
|
|
|
|
ipcMain.on('ondragstart', (event, filePath) => {
|
|
|
|
event.sender.startDrag({
|
|
|
|
file: filePath,
|
|
|
|
icon: '/path/to/icon.png'
|
|
|
|
})
|
|
|
|
})
|
|
|
|
```
|
2020-10-16 02:14:16 +00:00
|
|
|
|
2020-10-19 21:18:43 +00:00
|
|
|
After launching the Electron application, try dragging and dropping
|
2021-01-20 19:28:10 +00:00
|
|
|
the item from the BrowserWindow onto your desktop. In this guide,
|
2020-10-16 02:14:16 +00:00
|
|
|
the item is a Markdown file located in the root of the project:
|
|
|
|
|
|
|
|
![Drag and drop](../images/drag-and-drop.gif)
|
2021-05-19 00:55:24 +00:00
|
|
|
|
|
|
|
[`contextBridge`]: ../api/context-bridge.md
|