node.js将图片base64解码保存在服务器
设置base64大小,不写会报错!
server.use(bodyParser.json({
limit: '50mb'
}))
server.use(bodyParser.urlencoded({
limit: '50mb',
extended: true
}))
写接收base64码的接口
const fs = require("fs");
server.post('/upload', (req, res) => {
let imgData = req.body.load;
var base64 = imgData.replace(/\s/g, "+");
var base64Data = base64.replace(/^data:image\/\w+;base64,/, '')
console.log(base64Data);
var dataBuffer = Buffer.from(base64Data, 'base64')
fs.writeFile('./upload/image.png', dataBuffer, function (err) {
if (err) return
console.log('图片保存成功')
})
res.end('ok')
})