前端代码
let formData = new FormData(); formData.append('image', { uri: 'E:\\webroot\\file.png', type: 'image/png', name: 'file.png', }); fetch('http://localhost:3000/upload',{ method:'post', headers:{ 'Content-Type':'multipart/form-data', }, body:formData, }) .then((response) => response.json() ) .then((response)=>{ }) .catch((err)=>true);
nodejs端
let multer = require('multer');let upload = multer({dest:"public/uploads/"}).single('image');app.post('/upload', upload, function (req, res) {console.log('test')//根本就执行不到这里来,我看到uploads目录创建成功,但是文件没有保存,而且服务器端报500错误})
解决方案
把
headers
设置去掉!!你的前端代码formData构造的有问题
把你的错误贴出来