Created
October 25, 2017 05:45
-
-
Save madeinfree/a8ff571e1e3110ef80daacf666df9b3c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const express = require('express'); | |
const app = express(); | |
app.post('/image-upload', (req, res) => { | |
let body = ''; | |
let len = parseInt(req.get('content-length'), 10); | |
req.on('data', chunk => { | |
body += chunk; | |
}); | |
req.on('end', () => { | |
const chunkLen = parseInt(body.length, 10); | |
const json = JSON.parse(body); | |
var base64Data = json.url.replace(/^data:image\/png;base64,/, ''); | |
if (chunkLen === len) { | |
require('fs').writeFile('out.png', base64Data, 'base64', function(err) { | |
console.log(err); | |
}); | |
return res.status(200).send('Upload file success.'); | |
} | |
return res.status(200).send('Upload file failed.'); | |
}); | |
}); | |
app.listen(3001, () => { | |
console.log('Server listen on port 3001'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment