base64的形式爲「data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0。。。。」;
當接收到上邊的內容後,須要將data:image/png;base64,這段內容過濾掉,過濾成:「iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0。。。」;而後進行保存。app
app.post('/upload', function(req, res){ //接收前臺POST過來的base64 var imgData = req.body.imgData; //過濾data:URL var base64Data = imgData.replace(/^data:image\/\w+;base64,/, ""); var dataBuffer = new Buffer(base64Data, 'base64'); fs.writeFile("image.png", dataBuffer, function(err) { if(err){ res.send(err); }else{ res.send("保存成功!"); } }); });