我使用formdata上传图像,我也想上传包含数据的表单,我也在服务器上使用axios我得到图像,以objact的形式获取它
这是反应客户
const onSubmitToSend = async e => {
e.preventDefault();
const formData = new FormData();
formData.append('file', file);
formData.append('uploadedFile', uploadedFile);
try {
const res = await axios.post('http://localhost:4000/users/admin/upload', formData, {
headers: { 'Content-Type': 'multipart/form-data' }
});
const { fileName, filePath } = res.data;
setUploadedFile({ fileName, filePath });
} catch (err) {
if (err.response.status === 500) {
console.log('There was a problem with the server');
} else {
console.log(err.response.data.msg);
}
}
const data = { description, destination, createdAt, updatedAt, price }
await axios.post('http://localhost:4000/users/admin/upload', data, {
}).then((response) => { console.log('response', response) }
, (error) => { console.log('error', error) });
}
这是node.js服务器
我做2次axios 然后该文件被称为快速文件,无法保存表单数据
exports.adminSendFile = (req, res) => {
console.log('>>>>SendFile');
console.log(req.body);
console.log('>>>>SendBody');
var data = req.body
var file = req.files.file;
if (req.files === null) {
return res.status(400).json({ msg: 'No file uploaded' });
}
console.log(req.body, 'req.body');
file.mv(`${__dirname}/../uploads/${file.name}`, err => {
console.log("****HERE*****");
if (err) {
console.error(err);
return res.status(500).send(err)
} else {
console.log('file', file);
res.json({
fileName: file.name,
filePath: `/server/Servises/uploads/${file.name}`
})
}
})
console.log('new data', data),
vacations.create({
description: req.body.description,
destination: req.body.destination,
createdAt: req.body.createdAt,
updatedAt: req.body.updatedAt,
img: req.body.filePath,
price: req.body.price,
}).catch(err => {
res.status(500).send('Error -> ' + err);
})
}
即使试图将信息发送到DATABASE也给我带来空白行