Repository navigation
File Drop
For security reasons, browsers do not expose the file system path of a File object, e.g., one obtained via drag and drop or an <input type="file"> element. Electron provides webUtils.getPathForFile for this, but it is only accessible from a preload script.
Electron.NET registers a small preload script for every session, which exposes this function as window.electronNET.getPathForFile. It works regardless of the NodeIntegration, ContextIsolation, and Sandbox settings of the window, and it runs side by side with your own WebPreferences.Preload script.
getPathForFile returns an empty string for files that are not backed by the file system (e.g., created via new File()).
<div id="drop-zone">Drop files here</div>
<script>
const dropZone = document.getElementById('drop-zone');
dropZone.addEventListener('dragover', (e) => e.preventDefault());
dropZone.addEventListener('drop', (e) => {
e.preventDefault();
for (const file of e.dataTransfer.files) {
const path = window.electronNET.getPathForFile(file);
console.log(path);
}
});
</script>To process the paths in .NET, send them to the main process, e.g., via ipcRenderer.send (with node integration) and Electron.IpcMain.On, or post them to an endpoint of your ASP.NET Core app.
In Blazor, the drop event does not give you access to the File objects, so a small JavaScript helper is used to forward the paths to .NET:
// wwwroot/js/fileDrop.js
window.registerFileDrop = (element, dotNetRef) => {
element.addEventListener('dragover', (e) => e.preventDefault());
element.addEventListener('drop', (e) => {
e.preventDefault();
const paths = Array.from(e.dataTransfer.files)
.map((file) => window.electronNET.getPathForFile(file))
.filter((path) => path);
dotNetRef.invokeMethodAsync('OnFilesDropped', paths);
});
};@inject IJSRuntime JS
@implements IDisposable
<div @ref="dropZone" class="drop-zone">Drop files here</div>
<ul>
@foreach (var path in paths)
{
<li>@path</li>
}
</ul>
@code {
private ElementReference dropZone;
private DotNetObjectReference<FileDropZone>? selfRef;
private string[] paths = [];
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
selfRef = DotNetObjectReference.Create(this);
await JS.InvokeVoidAsync("registerFileDrop", dropZone, selfRef);
}
}
[JSInvokable]
public void OnFilesDropped(string[] droppedPaths)
{
paths = droppedPaths;
StateHasChanged();
}
public void Dispose() => selfRef?.Dispose();
}Note
Dropping a file anywhere outside an element that calls preventDefault() on dragover / drop makes Electron navigate to that file. Handle these events on document if you want to prevent this globally.
Want to contribute to this documentation? Please fork and create a PR! The Wiki is autogenerated from the /docs content in the repository.
-
Configuration
-
Startup-Methods
-
Debugging
-
Package Building
-
Secure Communication
-
Adding a
custom_main.js
-
File Paths (File Drop)
-
API Overview
-
Electron.App
-
Electron.Dialog
-
Electron.Menu
-
Electron.WindowManager
-
Electron.Shell
-
Electron.Screen
-
Electron.Notification
-
Electron.Tray
-
Electron.Clipboard
-
Electron.Dock
-
Electron.HostHook
-
Electron.IpcMain
-
Electron.GlobalShortcut
-
Electron.AutoUpdater
-
Electron.NativeTheme
-
Electron.PowerMonitor
custom_main.js