I'm attempting to upload a local file to a node js express server. My local client upload looks like this:
const file = await fs.readFileSync(`${desktopDir}/ai2html-output/home-pro-Artboard_1.jpg`);
const title = "Test";
const form = new FormData();
form.append("name", title);
form.append("file", file);
console.log(form);
form console log is:
dataSize: 0
maxDataSize: 2097152
pauseStreams: true
readable: true
writable: false
_boundary: "--------------------------148357794967581903667353"
_currentStream: null
_insideLoop: false
_overheadLength: 246
_pendingNext: false
_released: false
_streams: (6) ["----------------------------1483577949675819036673…
↵Content-Disposition: form-data; name="name"
↵
↵", "Test", ƒ, "----------------------------1483577949675819036673…file"
↵Content-Type: application/octet-stream
↵
↵", Buffer(42595), ƒ]
_valueLength: 42599
I then fetch to the server:
const response = await fetch('https://example.com/uploads/1234', {
method: 'POST',
headers: {
"Content-Type": `multipart/form-data`,
},
body: form });
In my express server:
const fileUpload = require('express-fileupload');
app.use(fileUpload());
router.post('/uploads/:id', (req, res) => {
let sampleFile;
let uploadPath;
if (!req.files || Object.keys(req.files).length === 0) {
return res.status(400).send('No files were uploaded.');
}
// The name of the input field (i.e. "sampleFile") is used to retrieve the uploaded file
sampleFile = req.files.sampleFile;
uploadPath = __dirname + '/var/www/vhosts/preview.inkto.art/httpdocs/html/' + sampleFile.name;
// Use the mv() method to place the file somewhere on your server
sampleFile.mv(uploadPath, function(err) {
if (err)
return res.status(500).send(err);
res.send('File uploaded!');
});
})
The response from the server says 400 'No files were uploaded' in the request header says:
[object FormData] for the payload.
Any idea why it's not recognizing the file? What am I missing?