如何 Post "multipart/form-data" 形成并从 Node.js 服务器获取文本字段值?

How to Post "multipart/form-data" Form and get the Text field values from the Node.js server?

Í 正在尝试使用 multer 上传文件。我可以上传文件,但不知何故无法使用 content/type 获取表单内的文本框值 "multipart/form-data".

<div class="container">
    <h1>File Upload</h1>
    <form action="/upload" method="POST" enctype="multipart/form-data" >
      <div class="file-field input-field">
        <div class="btn grey">
          <span>File</span>
          <input name="myImage" type="file" multiple="multiple"> 
        </div>
        <div class="file-path-wrapper">
          <input class="file-path validate" type="text">
        </div>        
      </div>
      <div ><input type="text" name="test"/></div>
      <button type="submit" class="btn">Submit</button>
    </form>
</div>

如何获取文本框的值

<div ><input type="text" name="test"/></div>

使用body.parser? 当我尝试

const {test} = req.body;

它给出了一个错误 TypeError: Cannot read 属性 'test' of undefined.

您需要将正文解析器包含到您的节点服务器中:

const bodyParser = require('body-parser');
app.use(bodyParser.json());       
app.use(bodyParser.urlencoded({ extended: true})); 

那么您应该可以访问正文中的表单数据,即 req.body.test