how to handle multipart/form-data in node.js


I am uploading image file from client side using multipart form data. I want to receieve and write it as a file in the server side using node.js.

        <form action="url" method="post" enctype="multipart/form-data">
            <input type="text" name="imageName">
            <input type="file" name="sam">

This is my client side code. How to handle this file in server side.

10/26/2016 12:18:30 PM

It is repeated question below link.

Uploading images using Node.js, Express, and Mongoose

Here is example:

// Expose modules in ./support for demo purposes
require.paths.unshift(__dirname + '/../../support');

 * Module dependencies.

var express = require('../../lib/express')
  , form = require('connect-form');

var app = express.createServer(
  // connect-form (
  // middleware uses the formidable middleware to parse urlencoded
  // and multipart form data
  form({ keepExtensions: true })

app.get('/', function(req, res){
  res.send('<form method="post" enctype="multipart/form-data">'
    + '<p>Image: <input type="file" name="image" /></p>'
    + '<p><input type="submit" value="Upload" /></p>'
    + '</form>');
});'/', function(req, res, next){

  // connect-form adds the req.form object
  // we can (optionally) define onComplete, passing
  // the exception (if any) fields parsed, and files parsed
  req.form.complete(function(err, fields, files){
    if (err) {
    } else {
      console.log('\nuploaded %s to %s'
        ,  files.image.filename
        , files.image.path);

  // We can add listeners for several form
  // events such as "progress"
  req.form.on('progress', function(bytesReceived, bytesExpected){
    var percent = (bytesReceived / bytesExpected * 100) | 0;
    process.stdout.write('Uploading: %' + percent + '\r');

console.log('Express app started on port 3000');

If your question is not solve then please visit this link . This is a nice article about file uploading.

5/23/2017 11:46:22 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow