Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bbottema/390852735943edecc9f1 to your computer and use it in GitHub Desktop.
Save bbottema/390852735943edecc9f1 to your computer and use it in GitHub Desktop.
A complete Express 4 example using multer to read base64 png data and reading the file back to the client as binary data
var express = require('express');
var fs = require('fs');
var multer = require('multer'); // v1.0.5
var app = express();
app.use(express.static('./public'));
var upload = multer(); // for parsing multipart/form-data
app.post('/testFormData', upload.array(), function(req, res) {
var base64Data = req.body.testdot;
console.log('writing file...', base64Data);
fs.writeFile(__dirname + "/upload/out.png", base64Data, 'base64', function(err) {
if (err) console.log(err);
fs.readFile(__dirname + "/upload/out.png", 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);
});
@kleavjames
Copy link

do you have the client side code? this is unclear for me. thanks

@Yasir5247
Copy link

How about uploading to amazon s3

@lucas-langa
Copy link

lucas-langa commented Apr 8, 2020

I had a similar problem even with this fix but then resolved by additionally stripping off the header part of the string like so

let base64String = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgA';
// Remove header
let base64Image = base64String.split(';base64,').pop();

@ShinJustinHolly3317
Copy link

this helps, thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment