简体   繁体   English

是否可以通过提供路径将文件从 Electron 应用程序发送到 Express 服务器?

[英]Is it possible to send a file from Electron app to Express server by providing the path?

This is what I'm trying to do:这就是我想要做的:

  1. User drags and drops a file into Electron用户将文件拖放到 Electron 中
  2. Electron uses a python script to convert it (child_process.exec) Electron 使用 python 脚本来转换它(child_process.exec)
  3. Conversion creates a file in the same directory as the original file转换在与原始文件相同的目录中创建一个文件
  4. Since I know the path (original file except different extension) the file gets uploaded onto the server, which stores it for future access.由于我知道路径(除了不同扩展名的原始文件),文件被上传到服务器,服务器存储它以备将来访问。

I'm stuck at step 4. Whatever I do I can't seem to retrieve the converted file.我被困在第 4 步。无论我做什么,我似乎都无法检索转换后的文件。 This is what I've tried:这是我尝试过的:

  let bodyFormData = new FormData();
  bodyFormData.append('model', createReadStream(filePath));

  axios({
    method: 'post',
    url: 'my/path',
    data: bodyFormData,
    headers: {
      'Content-Type': `multipart/form-data; boundary=${bodyFormData._boundary}`
    }
  })
    .then(function (response) {
      //handle success
      console.log(response);
    })
    .catch(function (response) {
      //handle error
      console.log(response);
    });

And this is what req looks like from express:这就是 express 中 req 的样子:

  body: { model: '[object Object]' },
  files: null,

I don't know if it's even possible.我不知道这是否可能。 If it's not, I would appreciate any advice on other ways of achieving what I'm trying to do.如果不是,我将不胜感激有关实现我正在尝试做的事情的其他方法的任何建议。 I hope my explanation was enough, thanks in advance!我希望我的解释足够了,在此先感谢!

I managed to fix it myself.我设法自己修复了它。 Firstly, yes, it is possible to read files from the file system as long as you've got appropriate privileges.首先,是的,只要您有适当的权限,就可以从文件系统中读取文件。

All I did was to convert the file stream to blob before sending and it worked.我所做的就是在发送之前将文件流转换为 blob 并且它起作用了。 I used this awesome npm package called stream-to-blob and it did the job perfectly.我使用了这个名为 stream-to-blob 的很棒的 npm 包,它完美地完成了这项工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM