Skip to content

Instantly share code, notes, and snippets.

@lxe
Created May 16, 2014 18:09
Show Gist options
  • Save lxe/321c079139046d5a419e to your computer and use it in GitHub Desktop.
Save lxe/321c079139046d5a419e to your computer and use it in GitHub Desktop.
var fs = require('fs')
, path = require('path')
, app = require('express')()
, Busboy = require('busboy');
function saveFiles(req, done) {
var busboy = new Busboy({ headers : req.headers });
busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
console.log(fieldname, file, filename, encoding, mimetype);
// Pipe to fs or db, or res, or anywhere you want!
file.pipe(fs.createWriteStream(path.join(__dirname, path.basename(filename))));
});
busboy.on('finish', done);
req.pipe(busboy);
}
app.post('/', function (req, res) {
saveFiles(req, function () {
res.send(200);
});
});
app.get('/', function (req, res) {
res.send(
'<html><head></head><body>\
<form method="POST" enctype="multipart/form-data">\
<input type="text" name="textfield"><br />\
<input type="file" name="filefield"><br />\
<input type="submit">\
</form>\
</body></html>');
});
app.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment