Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bbottema/3f79bc379a0705f77a76 to your computer and use it in GitHub Desktop.
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
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