- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经阅读了这里所有可能的答案,但我不确定问题可能是什么,因为它们都不起作用。我有一个上传单个图像的表单,它按预期工作,但在另一种表单上,我尝试上传多个文件并具有表单输入,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/
我正在开发一个 nodejs 项目快速应用程序。我的路线看起来像这样: router.get('/observe/:fileName', function(res, req){ var req
从 Facebook 初始成功登录并使用 passport-facebook 1.0.3 和 express 4.6.1 登录重定向回调后,req.session.passport 和 req.use
req.body有什么区别和 req.params.all()何时用于风帆 Controller ? 最佳答案 req.body是从请求正文中解析出来的任何内容,例如 POST 中的有效负载要求。 r
我想知道 req.query 和 req.body 有什么区别? 下面是一段代码,其中req.query用来。如果我使用 req.body 会发生什么而不是 req.query . 以下函数被调用作为
我以前问过类似的问题,但我注意到它在 Javascript 部分。对于现在可能出现的问题,我也有更具体的想法。 基本上,req.session.passport 在我的日志中是空的。每当我开始浏览我的
我不久前从某处盗取了这段代码,并想回来进一步了解它。 iv 在 pi 上运行它已经有一段时间了,没有任何问题,但是当我运行网络服务器时,我需要恢复操作系统的备份,自从这样做以来,我一直无法让它运行,如
Req.isAuthenticated()在登录后以及注册新用户后返回false。我需要做什么来修复它? 我有一个具有两个不同登录名的应用程序。一个用于“用户”,另一个用于“雇员”。 “用户”用户是我
我一直在拼凑来自几个不同教程的代码,以使用 MEAN 堆栈构建一个基本的待办事项应用程序,使用 node、express、angular 和 mongodb。一个教程介绍了为 GET、POST 和 D
我使用以下路由接收 url 中的参数: app.get('/:lang', function(req, res) { }); 我将推送 lang 的值并将其发送到一个函数,但我希望只能在用户不输入任何
根据条件,我需要销毁用户的当前 session ,并将他重定向到带有消息的登录页面。我使用 flash 来获得只显示一次的消息。除了这里,这在我的应用程序上无处不在,因为这里我在 req.sessio
Sample 'Advanced REST Client' Request 我正在使用 Postman 和高级 REST 客户端为以下代码创建基本 POST 请求 - 'use strict'; va
if (!req.session.cart) { req.session.cart = { styles: styles,
我正在尝试使用 NodeJs 脚本将文件上传到服务器。我正在尝试以下操作。 HTML Upload a zip file
通过 req.query[myParam] 和 req.params.myParam 获取 QUERY_STRING 参数有区别吗?如果是这样,我应该什么时候使用哪个? 最佳答案 鉴于这条路线 app
在 Shiny 的应用程序中,我使用 req 检查输入的有效性如果输入的要求为 TRUE(即不为空、不为 FALSE 等),则更新输出。 如果 req 我想运行一些代码是 FALSE,即使用 req(
我正在使用 Express 和 Body Parser。给定以下标题键: X-Master-Key 当我使用下面的代码片段时,它无法输出值 req.headers['X-Master-Key'] //
要么使用正文解析器 application/x-www-form-urlencoded body parser 或 json body parser 产生相同的结果。 这就是我调用 API 的方式 $
在nodejs中我们通常会做这样的事情: req.checkBody('name', 'Group name is required.').notEmpty(); 同样,我也做了这样的事情: req.
我正在使用 Multer 在 keystone 环境中解析多部分表单,并且无法访问路由 Controller 内的 req.body 和 req.file 数据 路线/index.js var key
我想了解这是为什么: req.session._id = doc._id; req.session.id = doc._id; console.log(typeof req.session._id);
我是一名优秀的程序员,十分优秀!