javascript - How to implement a Dropzone for folders in a ReactElectron app and process the folder contents? - Stack Overflow

Is there a way in electron based apps to let a user drop a folder onto a React-component and handle the

Is there a way in electron based apps to let a user drop a folder onto a React-component and handle the drop event so that the main process can fully access it and recursively scan the folder’s contents?

My setup so far:

  • the Renderer has a <DropZone> component with onDrop and onDragOver.
  • the drop event fires and i can see event.dataTransfer.files, but the path (webkitRelativePath) for dropped folders is empty also for files.
const folderDropped = async (event: React.DragEvent) => {
  event.preventDefault();

  // If a folder or file has been dragged
  if (event.dataTransfer.files.length > 0) {
    const filePath = event.dataTransfer.files[0].path;
    console.log("Drop event:", event.dataTransfer.files);
    console.log("Dropped file path:", filePath);

    // Call the IPC handler: readDroppedFile
    const result = await window.api.readDroppedFile(filePath, browserWindowId);
    console.log("readDroppedFile result:", result);

     /* Further Process:
       After the api call, the object is checked if its a folder and that it contains files
       of a specific type (for example, only PNG files). Once confirmed, the main process will scan through all the
       files within the folder recursively.
    */
  }
};

Visual Studio Code, which is also built with Electron, can accept dropped files and folders from the Explorer, so it should be possible for my app as well.

Additional details (optional):

  • Electron version: 35
  • React version: 19
  • Windows 11

Is there a way in electron based apps to let a user drop a folder onto a React-component and handle the drop event so that the main process can fully access it and recursively scan the folder’s contents?

My setup so far:

  • the Renderer has a <DropZone> component with onDrop and onDragOver.
  • the drop event fires and i can see event.dataTransfer.files, but the path (webkitRelativePath) for dropped folders is empty also for files.
const folderDropped = async (event: React.DragEvent) => {
  event.preventDefault();

  // If a folder or file has been dragged
  if (event.dataTransfer.files.length > 0) {
    const filePath = event.dataTransfer.files[0].path;
    console.log("Drop event:", event.dataTransfer.files);
    console.log("Dropped file path:", filePath);

    // Call the IPC handler: readDroppedFile
    const result = await window.api.readDroppedFile(filePath, browserWindowId);
    console.log("readDroppedFile result:", result);

     /* Further Process:
       After the api call, the object is checked if its a folder and that it contains files
       of a specific type (for example, only PNG files). Once confirmed, the main process will scan through all the
       files within the folder recursively.
    */
  }
};

Visual Studio Code, which is also built with Electron, can accept dropped files and folders from the Explorer, so it should be possible for my app as well.

Additional details (optional):

  • Electron version: 35
  • React version: 19
  • Windows 11
Share Improve this question asked Mar 10 at 16:46 silvio_lsilvio_l 211 silver badge4 bronze badges 2
  • Have you tried the webUtils.getPathForFile method? – yuanyxh Commented Mar 11 at 3:23
  • Yes, I tried that. Unfortunately, it results in an error on the frontend. ///> Uncaught ReferenceError: __dirname is not defined </// I get this error when I try to execute "webUtils.getPathForFile" on dataTransfer.files[0]. – silvio_l Commented Mar 12 at 11:37
Add a comment  | 

1 Answer 1

Reset to default 1

Ahh, I solved it now. I found the following: // Planned Breaking API Changes (32.0) >> Removed: File.path //. This can be found on the Electron website: "https://www.electronjs./docs/latest/breaking-changes#removed-filepath" So I have to go the way via a preload.js script.

// (renderer)
const file = event.dataTransfer.files[0].
electronAPI.getFilePath(file)

// (preload)
import {
  contextBridge,
  webUtils
} from 'electron'

contextBridge.exposeInMainWorld('electronAPI', {
  getFilePath(file) {
    return webUtils.getPathForFile(file)
  }
})

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744834275a4596168.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信