Last active
September 30, 2015 19:20
-
-
Save bbottema/3f79bc379a0705f77a76 to your computer and use it in GitHub Desktop.
A complete Express 4 example using multer to read multipart/form-data png file upload and reading the file back to the client as binary data
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'); | |
var busboy = require('connect-busboy'); // middleware for form/file upload | |
var fs = require('fs'); | |
var app = express(); | |
app.use(express.static('./public')); | |
app.use(busboy()); | |
app.post('/testFormDataBinary', function(req, res) { | |
req.pipe(req.busboy); | |
req.busboy.on('file', function (fieldname, file, filename) { | |
var tmpFilePath = __dirname + '/upload/' + filename; | |
console.log('writing ', filename, 'to', tmpFilePath); | |
var fstream = fs.createWriteStream(tmpFilePath); | |
file.pipe(fstream); | |
fstream.on('close', function (err) { | |
if (err) console.log(err); | |
fs.readFile(tmpFilePath, function(err, data) { | |
if (err) throw err; | |
console.log('reading file...', data.toString('base64')); | |
res.send(data); | |
}); | |
}); | |
}); | |
}); | |
var server = app.listen(8080, function() { | |
var host = server.address().address; | |
var port = server.address().port; | |
console.log('Server listening at http://%s:%s', host, port); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment