Created
July 17, 2015 15:01
-
-
Save karlhorky/c14a1afe631beddbc6e5 to your computer and use it in GitHub Desktop.
Simple File Upload with Express
This file contains 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 express = require('express'), | |
busboy = require('connect-busboy'), | |
app = express(), | |
fs = require('fs'), | |
router = express.Router(); | |
router.get('/', function(req, res) { | |
res.send('<form action="" method="post" enctype="multipart/form-data"><input type="file" name="displayImage"><input type="submit" name="Upload"></form>'); | |
}); | |
router.post('/', function(req, res) { | |
req.busboy.on('file', function(fieldName, file, filename) { | |
console.log(fieldName, file, filename); | |
file.on('data', function(data) { | |
var newPath = __dirname + "/" + filename; | |
fs.writeFile(newPath, data, function (err) { | |
if (err) throw err; | |
res.redirect("back"); | |
}); | |
}); | |
res.send('done'); | |
}); | |
req.pipe(req.busboy); | |
}); | |
app.use(busboy()); | |
app.use('/', router); | |
app.listen(8080); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment