如何从服务器端多个输入字段(Express.js/Node.js)查找数据?

时间:2022-06-20 15:59:45

I have a form with multiple description text fields, file chose field and submit button for the Cross-Domain request (CORS variant):

我有一个具有多个描述文本字段的表单,文件选择字段并提交跨域请求的按钮(CORS变体):

  <form class="upload" id="upload_form" enctype="multipart/form-data" method="post" action="http://localhost:3001/upload/1234567890">

      <div class="row-fluid">

        <div class="span5 row-fluid" id="description" style="margin-left:0px;">
          <div>
              <label>Title</label>
              <input class="span12" type="text" placeholder="Title" id="description_title" name="description_title"/>
              <label>Author</label>
              <input class="span12" type="text" placeholder="Author" id="description_author" name="description_author"/>
              <label>Tags</label>
              <input class="span12" type="text" placeholder="Tags" id="description_tags" name="description_tags"/>
              <label>Description</label>
              <textarea class="span12" id="description_textarea" name="description_textarea" rows="5" style="resize:none"></textarea>

              <div id="buttons" class="row-fluid" style="margin-top: 5px">
                  <div class="span12">
                    <span class="span5 btn btn-primary btn-file" id="chose_files_btn" onclick="filechose_button.click()">chose files
                      <input id="filechose_button" type="file" name="fileData" data-url="http://localhost:3001/upload/1234567890"/></span>
                    <button id="upload_button" type="submit" name="upload" class="span5 offset2 btn btn-success" disabled="true" onclick="$('#upload_form').trigger('upload_fired');">upload</button>
                  </div> <!-- span12 -->
              </div> <!-- buttons -->
          </div> <!-- well -->
        </div> <!-- video_description -->
      </div> <!-- row-fluid -->

   </form>

The Node.js (Express.js) server has a route:

的节点。js (Express.js)服务器有一条路径:

app.post('/upload/:id', function(req, res){
    console.log(req.fields);
        ...
});

The problem is, I can not find the data from input fields: req.fields gets undefined.

问题是,我无法从输入字段req中找到数据。字段被定义。

How can I find the data (text description and a file) from the req on the server?

如何从服务器上的req中找到数据(文本描述和文件)?


Update:

Great! req.body really get access to all the fields, BUT:

太棒了!要求的事情。身体真的可以进入所有的领域,但是:

console.log(req.body)

prints:

打印:

{ description_title: 'aaa',
  description_author: 'bbb',
  description_tags: 'ccc',
  description_textarea: 'ddd',
  upload: '' }

but where is the fileData field? I mean, where the file I upload with the description (aforementioned fields)?

但是文件数据字段在哪里?我的意思是,我上传的描述文件(前面提到的字段)在哪里?

Update 2: Done!

Files are not in the body attribute, but in the req.files

文件不在body属性中,而在req.files中

Thanks for Adam and andyfan!

谢谢Adam和andyfan!

2 个解决方案

#1


2  

As said by Adam if you see these two lines

正如亚当所说,如果你看到这两条线

app.use(express.bodyParser());
app.use(express.methodOverride());

in app.js file of express, then you can read fields as given below.

在express的app.js文件中,可以读取如下所示的字段。

app.post('/upload/:id', function(req, res){
    console.log(req.body.description_title);
    console.log(req.body.description_author);
    console.log(req.body.description_tags);
    console.log(req.body.description_textarea);
    console.log(req.files);
    res.send("Done!");
});

You can read more about how to handle files in express at this link http://howtonode.org/really-simple-file-uploads

您可以在此链接http://howtonode.org/re- fileupload中阅读更多关于如何处理文件的信息。

#2


1  

You want req.body so long as you have express.methodOverride() in your configuration before the routes.

你想要的要求。只要您在路径之前的配置中有express.method doverride()就可以了。

In your route try console.log(req.body); to see what object you get there. For the route parameters it is req.params.

在您的路线上尝试console.log(req.body);看看你得到了什么物体。对于路径参数,它是req.params。

console.log(object); can be your best friend when trying to find the objects your looking for and the data structure they are contained in.

console.log(对象);当你试图找到你寻找的对象和它们所包含的数据结构时,你可以成为你最好的朋友。

#1


2  

As said by Adam if you see these two lines

正如亚当所说,如果你看到这两条线

app.use(express.bodyParser());
app.use(express.methodOverride());

in app.js file of express, then you can read fields as given below.

在express的app.js文件中,可以读取如下所示的字段。

app.post('/upload/:id', function(req, res){
    console.log(req.body.description_title);
    console.log(req.body.description_author);
    console.log(req.body.description_tags);
    console.log(req.body.description_textarea);
    console.log(req.files);
    res.send("Done!");
});

You can read more about how to handle files in express at this link http://howtonode.org/really-simple-file-uploads

您可以在此链接http://howtonode.org/re- fileupload中阅读更多关于如何处理文件的信息。

#2


1  

You want req.body so long as you have express.methodOverride() in your configuration before the routes.

你想要的要求。只要您在路径之前的配置中有express.method doverride()就可以了。

In your route try console.log(req.body); to see what object you get there. For the route parameters it is req.params.

在您的路线上尝试console.log(req.body);看看你得到了什么物体。对于路径参数,它是req.params。

console.log(object); can be your best friend when trying to find the objects your looking for and the data structure they are contained in.

console.log(对象);当你试图找到你寻找的对象和它们所包含的数据结构时,你可以成为你最好的朋友。