最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Axios以格式数据格式上传jpg,响应数据:{错误:'多部分:未找到边界'}

运维笔记admin9浏览0评论

Axios以格式数据格式上传jpg,响应数据:{错误:'多部分:未找到边界'}

Axios以格式数据格式上传jpg,响应数据:{错误:'多部分:未找到边界'}

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

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做同样的事情?错误消息很长,但主要提示。应该是响应数据:{错误:“多部分:未找到边界”}。

以下是一些内容:

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)
}...
回答如下:

固定:

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

评论列表(0)

  1. 暂无评论