You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This does not handle multipart bodies, due to their complex and typically large nature.
For multipart bodies, you may be interested in the following modules
在学习node时发现书上说body-parser中间件可以解析文件,自己实验了之后发现req.files仍然是undefined,查了一下官方文档才发现,body-parser不提供文件类型的解析:
于是用了推荐的multer中间件,语法也很简单:
single是最简单的上传单个文件,需要注意的是,single里面的字符串必须要和表单中定义的file字段的名字一样才行,否则会报错,随便上传一张图片,看一下输出结果:
可以看到,对于单个文件,req.file直接等于该文件对象,里面保存了关于文件信息的各种字段。最后会发现上传的文件保存格式是二进制文件,无法直接使用,这时可以用fs.rename方法来重命名文件和文件路径,使其变成可访问的文件
如果要上传多个文件,也是同理,具体参考multer官方文档
The text was updated successfully, but these errors were encountered: