title: Upload file with node js and store form information in database(mysql)
body: <p>I'm building a website where user needs to fill a form with information as first name, last name, profile picture, number etc... I've already code the form and I'm looking a way with node js to upload the image in a specific directory (or a default directory) and store the information that the users filled in my database. I use node js and Express with mysql.</p>
  
  <p>UPDATE: </p>
  
  <p>(I used formidable package and util package).
  Please notice that for the image part, I only need the path( you will see it on the ouput section).</p>
  
  <p>form code:</p>
  
  <pre><code>&lt;form method="post" action="upload" enctype="multipart/form-data"&gt;
      &lt;fieldset&gt;
         &lt;input type="file" accept="image/*" name="image" required /&gt;&lt;br/&gt;
         &lt;label for="Prenom"&gt;Prénom: &lt;/label&gt;
         &lt;input type="text" name="user_prenom" id="Prenom" required /&gt;&lt;br/&gt;
         &lt;label for="Nom"&gt;Nom: &lt;/label&gt;
         &lt;input type="text" name="user_nom" id="Nom" required /&gt;&lt;br/&gt;
         &lt;label for="Mail"&gt;E-Mail: &lt;/label&gt;
         &lt;input type="email" name="user_mail" id="Mail" required/&gt;&lt;br/&gt;
         &lt;label for="Pays"&gt;Pays: &lt;/label&gt;
         &lt;input type="text" name="user_pays" id="Pays" required/&gt;&lt;br/&gt;
         &lt;label for="Ville"&gt;Ville: &lt;/label&gt;
         &lt;input type="text" name="user_ville" id="Ville" required/&gt;&lt;br/&gt;
         &lt;label for="Num"&gt;Numéro: &lt;/label&gt;
         &lt;input type="tel" name="user_telephone" id="Num" /&gt;&lt;br/&gt;
         &lt;input type="submit"  /&gt;
    &lt;/fieldset&gt;
   &lt;/form&gt;
  </code></pre>
  
  <p>handle:</p>
  
  <pre><code>router.post('/upload', function(req, res) {
  processFormFieldsIndividual(req, res);
  });
  
  function processFormFieldsIndividual(req, res) {
  //Store the data from the fields in your data store.
  //The data store could be a file or database or any other store based
  //on your application.
  var fields = [];
  var form = new formidable.IncomingForm();
  form.uploadDir = "/public/photo_utilisateurs";
  //Call back when each field in the form is parsed.
  form.on('field', function (field, value) {
      fields[field] = value;
  });
  //Call back when each file in the form is parsed.
  form.on('file', function (name, file) {
      fields[name] = file;
      //Storing the files meta in fields array.
      //Depending on the application you can process it accordingly.
  });
  
  //Call back for file upload progress.
  form.on('progress', function (bytesReceived, bytesExpected) {
      var progress = {
          type: 'progress',
          bytesReceived: bytesReceived,
          bytesExpected: bytesExpected
      };
      //Logging the progress on console.
      //Depending on your application you can either send the progress to client
      //for some visual feedback or perform some other operation.
  });
  
  //Call back at the end of the form.
  form.on('end', function () {
  
      res.writeHead(200, {
          'content-type': 'text/plain'
      });
      res.write('received the data:\n\n');
      res.end(util.inspect({
          fields: fields
      }));
  });
  // var user = JSON.parse(fields);
  // connection.query('INSERT INTO Utilisateurs (user_nom, user_prenom, user_mail, user_phone, user_pays, user_ville) VALUES ("' + user.user_nom + '", "' + user.user_prenom + '", "' + user.user_mail + '", "' + user.user_pays + '", "' + user.user_ville + '", "' + user.user_telephone + '")',
  //     function selectCb(err, results, fields) {
  //         if (err) throw err;
  //         else res.send('success');
  // });
  form.parse(req);
  }
  </code></pre>
  
  <p><strong>The Output</strong></p>
  
  <p>received the data:</p>
  
  <pre><code>{ fields: 
      [ user_prenom: 'dfw',
        user_nom: 'efwe',
        user_mail: 'efew@fref',
        user_pays: 'efwe',
        user_ville: 'efwe',
        user_telephone: '4380564324',
        image: File {
        domain: null,
        _events: {},
        _eventsCount: 0,
        _maxListeners: undefined,
        size: 518,
        path: '/tmp/upload_e611ea0745206682b26c208d816dc604',
        name: '1462507431_play_now_sign_youtube_video.svg',
        type: 'image/svg+xml',
        hash: null,
        lastModifiedDate: Mon May 09 2016 00:16:24 GMT-0400 (EDT),
       _writeStream: [Object] } 
      ] 
    } 
  </code></pre>