[英]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:这就是我想要做的:
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.