2

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

  <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:

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.

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


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

Thanks for Adam and andyfan!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
static
  • 8,126
  • 15
  • 63
  • 89
  • Post is similar to http://stackoverflow.com/questions/5710358/how-to-get-post-query-in-express-node-js – dvishal Apr 27 '13 at 00:57

2 Answers2

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.

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

naquiuddin
  • 1,000
  • 2
  • 12
  • 25
1

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

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(object); can be your best friend when trying to find the objects your looking for and the data structure they are contained in.

Adam
  • 1,059
  • 9
  • 15