如何发布“multipart/form-data"从 Node.js 服务器形成并获取文本字段值? [英] How to Post "multipart/form-data" Form and get the Text field values from the Node.js server?

查看:133
本文介绍了如何发布“multipart/form-data"从 Node.js 服务器形成并获取文本字段值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Í 正在尝试使用 multer 上传文件.我可以上传文件,但不知何故无法使用内容/类型获取表单内的文本框值多部分/表单数据".

<h1>文件上传</h1><form action="/upload" method="POST" enctype="multipart/form-data" ><div class="file-field input-field"><div class="btn 灰色"><span>文件</span><input name="myImage" type="file" multiple="multiple">

<div class="file-path-wrapper"><input class="file-path validate" type="text">

<div ><input type="text" name="test"/></div><button type="submit" class="btn">提交</button></表单>

如何获取文本框的值

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

使用 body.parser?当我尝试

const {test} = req.body;

它给出了一个错误类型错误:无法读取未定义的属性 'test'.

解决方案

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

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

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

Í'm trying to upload a file using multer. I can upload the file but somehow unable to get the text box values inside the form with the 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>

How can I get the value of the textbox

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

using body.parser? when I try

const {test} = req.body;

it gives an error TypeError: Cannot read property 'test' of undefined.

解决方案

You need to include body parser to your node server:

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

Then you should have access to form data in body i.e. req.body.test.

这篇关于如何发布“multipart/form-data"从 Node.js 服务器形成并获取文本字段值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆