req.files not working node.js - express

僤鯓⒐⒋嵵緔 提交于 2019-12-01 21:42:31

You need to add enctype="multipart/form-data" to the form

Besides what @Jani said, you have an error in your app:

app.use(express.bodyParser({uploadDir: './public/img'}));
app.use(express.multipart());

This basically translates to:

app.use(express.json());
app.use(express.urlencoded());
app.use(express.multipart({uploadDir: './public/img'}));
app.use(express.multipart());

So no need for the last multipart middleware.

Docs:

http://expressjs.com/api.html#bodyParser

Instead of calling express.bodyParser() consider the alternatives mentioned here: https://github.com/senchalabs/connect/wiki/Connect-3.0

In my case, as Connect will remove multipart middleware compatibility, a warning appears every time I start node server.

connect.multipart() will be removed in connect 3.0
visit https://github.com/senchalabs/connect/wiki/Connect-3.0 for alternatives
connect.limit() will be removed in connect 3.0

I've tested connect-multiparty and req.files is initialized fine. https://github.com/andrewrk/connect-multiparty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!