Robert Lui
Robert Lui

Reputation: 55

Axios upload jpg in form data format, response data: { error: 'Multipart: Boundary not found' }

I want to upload a jpg to a link with axios but failed. I doubt that is there sth wrong with my form data, I dont know how to check that. I do it in this way:

let form = new FormData();
form.append('screenshot',fs.createReadStream(`xxx.jpg`));

screenshot is the key while the next one is the jpg that I want to post. Here is my post method:

const config = { headers: { 'Content-Type': 'multipart/form-data' } };
yield axios.post(url,form,config).then....catch....

I have successfully upload the jpg through postman: enter image description here

How can I do the same thing with NodeJS in VScode?
The error message is very long but main pt. should be response data: { error: 'Multipart: Boundary not found' }.

Here is some content in sever:

const upload = multer({
//dest: 'screenshots',
limits: {
    fileSize: 2000000 //number in bytes
},
fileFilter(req, file, cb) {
    if (!file.originalname.match(/\.(png|jpg|jpeg)$/)) {
        return cb(new Error('File must be of png/jpg/jpeg type.'))
    }
    cb(undefined, true)
}

router.post('/upload/:id/screenshot', findCasebyID, upload.single('screenshot'), async (req, res) => {
try {
        req.case.screenshot = req.file.buffer
        await req.case.save()
        res.send()
} catch (e) {
    res.status(409).send(e)
}...

Upvotes: 1

Views: 1661

Answers (1)

Robert Lui
Robert Lui

Reputation: 55

I managed to solve it with this:

const config = { headers: { 'Content-Type': `multipart/form-data; boundary=${form._boundary}`, } };

Upvotes: 2

Related Questions