最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get file object filepath from electron main process? - Stack Overflow

programmeradmin5浏览0评论

I have a react electron app where the user can drag and drop files on the front end react component, and those files are sent to the backend via an ipc render function. I need to be able to get the full filepath for these dropped files from within the backend main.js electron process, but I can't figure out how to do so.

  const handleDrop = async (event) => {
    // Get dropped files
    event.preventDefault();
    const droppedFiles = Array.from(event.dataTransfer.files);
    console.log('Dropped files:', droppedFiles);

    // Send dropped files to backend main.js process
    window.api.send('get-filepaths', droppedFiles);

    // Handle returned file paths
    window.api.receive('get-filepaths-response', ( filepathsRsp ) => {
      console.log('Received file paths:', filepathsRsp);
    });
}

You can see on the front end my react app prints out the dropped file details, and filepath is not there.

Then in the main.js backend, the filepaths are received, but how do I in this main.js code below get the filepath for these files?

ipcMain.on('get-filepaths', (event, files) => {
  console.log('get-filepaths: ', files);

  // Get filepaths for each file
  files.forEach(file => {
    console.log('file = ', file.name)
  });

  // Send filepaths back to front end
  event.reply('get-filepaths-response', ['filepath1','filepath2']);
});
get-filepaths:  [ {}, {}, {} ]
file =  undefined
file =  undefined
file =  undefined

How can I get the filepath from these files in my backend main.js? I see in the electron docs online a section for how to do this with Renderer Process Modules, but I cant find any info on how to do this with Main Process Modules

All my code is on the main branch here:

发布评论

评论列表(0)

  1. 暂无评论