I’m trying to post a local zip file to an endpoint using axios
to be used in an async code from the main.
This works well with Postman.
The endpoint accept a file and a hash as str.
static async sendConf() {
const dataFolder =
process.env.NODE_ENV === 'development'
? path.join(__dirname, '../../data')
: path.join(app.getPath('userData'), '/data');
await Utilities.zipDirectory(
dataFolder,
path.join(dataFolder, 'conf.zip'),
).then(async (r) => {
const fs = require('fs');
const form_data = new FormData();
fs.readFile(path.join(dataFolder, 'conf.zip'), async (error, data) => {
form_data.append('hash', TOKEN);
const bb = new Blob([data]);
form_data.append('file', bb, {
contentType: 'multipart/form-data',
filename: 'conf.zip',
});
const response = await Utilities.postAxios('upload-conf', {
data: form_data,
})
.then((r) => {
console.log(`done`);
})
.catch((e) => {
console.log(`sendConf err : ${e}`);
});
console.log(`sendConf : ${response}`);
});
});
}
And postAxios looks like this :
static async postAxios(url, params) {
const obj = await Utilities.readConfFile('settings');
const authAxios = axios.create({
baseURL: obj.generalSettings.warehouse,
});
try {
const { data } = await authAxios.post(
url,
{ ...params },
{
headers: {
'Content-Type': 'multipart/form-data',
},
},
);
return data;
} catch (error) {
console.log(error);
}
}
It gives the error below, any help ?
TypeError: source.on is not a function
at Function.DelayedStream.create (/project/node_modules/delayed-stream/lib/delayed_stream.js:33:10)
at FormData.CombinedStream.append (/project/node_modules/combined-stream/lib/combined_stream.js:45:37)
at FormData.append (/project/node_modules/form-data/lib/form_data.js:75:3)
at FormData.defaultVisitor (/project/node_modules/axios/lib/helpers/toFormData.js:175:14)
at each (/project/node_modules/axios/lib/helpers/toFormData.js:198:73)
at Object.forEach (/project/node_modules/axios/lib/utils.js:267:10)
at build (/project/node_modules/axios/lib/helpers/toFormData.js:197:11)
at toFormData (/project/node_modules/axios/lib/helpers/toFormData.js:214:3)
at Object.transformRequest (/project/node_modules/axios/lib/defaults/index.js:84:16)
at transform (/project/node_modules/axios/lib/core/transformData.js:22:15)
at Axios.request (/project/node_modules/axios/lib/core/Axios.js:45:41)
at async Function.postAxios (/project/src/main/utilities.ts:114:24)
at async /project/src/main/utilities.ts:297:26
Post local file to an endpoint API using Electron 26.2.1 and axios 1.7.7
2
Answers
This implementation did the trick, for whom can't change versions :
With :
import { blob } from 'node:stream/consumers';
The error you’re seeing (TypeError: source.on is not a function) occurs because Axios expects a Node.js Readable stream when working with file uploads, but a Blob (which is typically used in browser environments) is being provided. Here’s how you can fix the code by using Node.js fs streams instead
server.js
client.js
package.json