Skip to content

Instantly share code, notes, and snippets.

@scottoffen
Created July 23, 2015 19:52
Show Gist options
  • Save scottoffen/65744677c20816131483 to your computer and use it in GitHub Desktop.
Save scottoffen/65744677c20816131483 to your computer and use it in GitHub Desktop.
Node.js File Uploads

On the frontend, use dropzone.js, on the backend, use connect-express.

Node Route Example

module.exports.post = function (req, res, next)
{
	console.log('request:');

	var fstream;
	req.pipe(req.busboy);
	req.busboy.on('file', function (fieldname, file, filename)
	{
		console.log('Uploading: ' + filename);
		fstream = fs.createWriteStream('./public/uploads/' + filename);
		file.pipe(fstream);
		fstream.on('close', function ()
		{
			res.redirect('back');
		});
	});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment