【问题标题】:Axios upload jpg in form data format, response data: { error: 'Multipart: Boundary not found' }axios以表单数据格式上传jpg,响应数据:{error: 'Multipart: Boundary not found' }
【发布时间】:2020-02-10 19:27:48
【问题描述】:

我想用 axios 将 jpg 上传到一个链接,但失败了。 我怀疑我的表单数据有问题,我不知道如何检查。 我是这样做的:

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

截图是关键,下一张是我要发布的jpg。 这是我的发帖方法:

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

我已经通过邮递员成功上传了jpg:

如何在 VScode 中使用 NodeJS 做同样的事情?
错误消息很长,但主要是 pt。应该是响应数据:{ error: 'Multipart: Boundary not found' }。

这是服务器中的一些内容:

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)
}...

【问题讨论】:

    标签: javascript node.js axios postman multipartform-data


    【解决方案1】:

    我设法解决了这个问题:

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

    【讨论】:

    • 谢谢。从请求迁移到 axios 时遇到了完全相同的问题。似乎请求会自动设置边界,而对于 axios,您必须手动添加标头。
    猜你喜欢
    • 1970-01-01
    • 2020-04-05
    • 2022-11-29
    • 1970-01-01
    • 1970-01-01
    • 2016-11-21
    • 2019-03-25
    • 2020-12-04
    • 2019-08-06
    相关资源
    最近更新 更多