Created
May 16, 2014 18:09
-
-
Save lxe/321c079139046d5a419e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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