I'm making an app which consumes an API I wrote, thing is that, I need the client to send an image to the API and then save it server-side, I successfully sent an image using a file input and the following script:
const upload = _ => {
let form = new FormData();
form.append("file", document.getElementById("my-file-selector").files[0])
fetch('http://localhost:3377/me/uploadPfp', {
method: 'POST',
headers: {
"Authorization": "<%= locals.user.token %>",
"Content-Type": "application/x-www-form-urlencoded"
},
body: form,
}).then(
response => response.json()
).then(
success => console.log(success)
).catch(
error => console.log(error)
);
};
Server-side it seems to work, but I'm unable to save it using fs.writeFile()
, it returns this error:
TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView.
But when I console.log the received file, this happens: Receipt Image