1

I have a image saved in a buffer I would like to upload to a webserver without need to saved it to disk. I am using Axios as the lib for handling the post request

My code for posting is as follows

const payload = new FormData();
payload.append('file', buffer, { filename: 'unknown.png', contentType: 'image/png', knownLength: buffer.toString().length });
const token = await RetreiveToken();
const resp = await Axios.post(`https://api.novastudios.tk/${endpoint}`, payload, {
  headers: {
    ...payload.getHeaders(),
    'Authorization': token
  },
  maxBodyLength: 20971520,
  maxContentLength: 20971520,
});

The response from the server is a 500, upon having the web server log the incoming file I found that no file data was being sent (But the server was receiving a proper length header). I have tried converting the buffer to a Readable, and a Duplex but again nothing. I also added the filename, contentType and length parts but nothing either. Using electron + node.js for client and asp.net api for server)

Nova1545
  • 81
  • 1
  • 8
  • Did you try to add a test route to receive such file and send a test request to it to verify either a file content is received or not? – Anatoly Dec 08 '21 at 19:02
  • @Anatoly Yes I have, it works with a normal file but the moment I change it to using a Buffer it doesn't work. Ive also confirmed it works using the API's testing page aswell – Nova1545 Dec 08 '21 at 19:35
  • So it works sending a buffer to a test route? Did I get you right? – Anatoly Dec 08 '21 at 19:46
  • @Anatoly No, it doesn't, the route gets the request but it doesn't receive the buffer content. Sorry for making that kind of confusing – Nova1545 Dec 08 '21 at 19:57
  • Did you add the multer middleware on receiving route locally? – Anatoly Dec 10 '21 at 14:03
  • No, but Node.js is only for the client side, the server is a ASP.net API – Nova1545 Dec 11 '21 at 19:43
  • To test if you send a file correctly, please add and setup multer for your local node,js and a test receiving route. – Anatoly Dec 12 '21 at 10:56

0 Answers0