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}`, } };