Last active
December 14, 2022 14:34
-
-
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
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 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); | |
}); |
How about uploading to amazon s3
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();
this helps, thank you
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
do you have the client side code? this is unclear for me. thanks