gpt4 book ai didi

javascript - 对于多个文件上传,req.file 未定义

转载 作者:行者123 更新时间:2023-12-01 01:34:11 27 4
gpt4 key购买 nike

我已经阅读了这里所有可能的答案,但我不确定问题可能是什么,因为它们都不起作用。我有一个上传单个图像的表单,它按预期工作,但在另一种表单上,我尝试上传多个文件并具有表单输入,req.file 始终未定义,图像数据最终出现在 req.body 中。所以我认为也许存在 multer 无法获取文件数据的问题......?

我在几个地方读到,body-parser 可能无法与 multer 配合使用,但不幸的是我需要它们。

路线

const destination = './public/uploads/posts';
const storage = multer.diskStorage({
destination: destination,
filename: (req, file, cb) => {
cb(null, file.originalname);
},
});
const upload = multer({storage}).array('images');

router.post('/posts', (req, res) => {

upload(req, res, (err) => {
if(err) {
console.log('errors', err)
res.json({success: false, error: err})
} else {
if(req.files === undefined) {
console.log('error: Images returned undefined');
} else {
const { title, description, url, auth_name, auth_id } = req.body;

if(!title || !description) {
return res.json({
success: false,
error: 'All fields required',
message: 'Title and description fields are required'
});
}

// path we store in the db
let imageLocation = [];
const post = new Post();

for (const file of req.files) {
imageLocation.concat('/uploads/posts/' + file.filename)
}

post.title = title;
post.description = description;
post.url = url;
post.author_name = auth_name;
post.author = auth_id;
post.images = imageLocation;
post.save((err, post) => {
if(err) {
return res.json({ success: false, error: err, message: 'post failed' });
} else {
return res.json({ success: true, message: "added new post", post: post });
}
});
}
}
});
});

在 react 组件中发布

我的图像设置为从 DropZone 包获取的状态(我在没有 DropZone 的情况下进行了测试,结果相同。我确保输入名称为“images”

submitNewPost = () => {
const { title, description, url, images } = this.state;
const auth_id = this.props.author_id;
const auth_name = this.props.author_name;

const formdata = new FormData();
formdata.append('title', title);
formdata.append('description', description);
formdata.append('url', url);
formdata.append('author_name', auth_name);
formdata.append('author', auth_id);
formdata.append('images', images);

fetch('/api/posts', {
method: 'POST',
body: formdata
}).then(res => res.json())
.then((res) => {
if (!res.success) {
this.setState({ message: res.message });
} else {
this.setState({
title: '',
description: '',
url: '',
images: '',
message: res.message
});
socket.emit('newPost', res.post);
}
});
}

更新

对此提供一些帮助 thread我设法使用现有的路线和 postman 上传图像。当我使用表单测试路线时,我仍然得到一个空的 req.file 并且没有上传。

我在我的设置中添加了express-multipart-file-parser,我认为这让我更接近解决这个问题。

服务器

这是我添加到 server.js 中的内容

 import { fileParser } from 'express-multipart-file-parser';
app.use(fileParser({
rawBodyOptions: {
limit: '15mb', //file size limit
},
busboyOptions: {
limits: {
fields: 20 //Number text fields allowed
}
},
}));

最佳答案

要正确填充req.files,您需要使用formdata.append,如下所示:

images.forEach(image => {
formdata.append('images', image);
})

关于javascript - 对于多个文件上传,req.file 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52990328/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com