Nodejs file upload with busboy

File upload is the basic requirement of any application. So today we will learn how to upload file in nodejs. So let’s get start with the file upload in nodejs

I am using express framework and busboy to handle the form, You can use any other framework as per your requirement

Dependencies

  • express
  • busboy

Run the below command to install the dependencies

npm install express

npm install busboy

Step 1 – Export Dependencies

var express = require('express'),
var Busboy = require('busboy')

Step 2 – Creating Form

To upload file we need a form. We will create form with `file input` element that allows to us to choose the file and a button to submit the form.

app.get('/', function (req, res) {
  	res.writeHead(200, {'Content-Type': 'text/html'});
	res.write('<form action="fileupload" method="post" enctype="multipart/form-data">');
	res.write('<input type="file" name="filetoupload"><br>');
	res.write('<input type="submit">');
	res.write('</form>');
	return res.end();
})

Make sure your form must have enctype="multipart/form-data" attribute and form method should be post

Step 3 – Handle Form Submit

Here we will handle the form, We will parse the form and store the file in the folder. To send the file in the server we must have the post method in the form. We will create a route with the post method

app.post('/fileupload', function (req, res) {
    var busboy = new Busboy({ headers: req.headers });
    busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {

      var saveTo = path.join(__dirname, 'uploads/' + filename);
      file.pipe(fs.createWriteStream(saveTo));
    });

    busboy.on('finish', function() {
      res.writeHead(200, { 'Connection': 'close' });
      res.end("That's all folks!");
    });
    
    return req.pipe(busboy);	
});

After completing above steps your file will look like this

var http = require('http'),
	express = require('express'),
	Busboy = require('busboy'),
	path = require('path'),
    fs = require('fs');

var app = express();

app.get('/', function (req, res) {
  	res.writeHead(200, {'Content-Type': 'text/html'});
	res.write('<form action="fileupload" method="post" enctype="multipart/form-data">');
	res.write('<input type="file" name="filetoupload"><br>');
	res.write('<input type="submit">');
	res.write('</form>');
	return res.end();
})

app.post('/fileupload', function (req, res) {
	var busboy = new Busboy({ headers: req.headers });
    busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {

      var saveTo = path.join(__dirname, 'uploads/' + filename);
      file.pipe(fs.createWriteStream(saveTo));
    });

    busboy.on('finish', function() {
      res.writeHead(200, { 'Connection': 'close' });
      res.end("That's all folks!");
    });

    return req.pipe(busboy);	
});

app.listen(8080);
 

Click here to know more about the busboy

Click here to know more about the express

Preview:

If you have found this article useful, share this article with your friends and leave comment if you have any question. Your responses are also highly appreciated.

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *