简体   繁体   English

使用Node.js的Multer无法正常工作

[英]Multer with Node.js not working

Okay, here is my form: 好的,这是我的表格:

<form action="/measure" method="post">
  <input type="file" name="thisfile"/>
  <input type="submit" value="Give us that file!"/>
</form>

And my server: 而我的服务器:

var express = require("express"),
    multer = require('multer'),
    app = express(),
    upload = multer({ dest: "./uploads/" });

app.post("/measure", upload.single("thisfile"), function (req, res) {
  console.log(req.file);
  //other stuff
});

When I submit the form to my server, req.file is undefined. 当我将表单提交到服务器时, req.file是未定义的。

Wow, wrote you code from scratch assuming a few things but I found the error. 哇,假设有几件事,从头开始编写代码,但我发现了错误。 In the Multer documentation, it says that Multer will not process a form that is not multipart. 在Multer文档中,它说Multer不会处理不是多部分的表单。 So you have to add that to your form (enctype="multipart/form-data"): 因此,您必须将其添加到表单中(enctype =“ multipart / form-data”):

<form action="/measure" method="post" enctype="multipart/form-data">
  <input type="file" name="thisfile"/>
  <input type="submit" value="Give us that file!"/>
</form>

With that it should work. 有了它应该工作。 Let me know if this helped you. 让我知道这是否对您有帮助。 PS: Here is the documentation: https://www.npmjs.com/package/multer PS:这是文档: https : //www.npmjs.com/package/multer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM