return true}else{if(aa[aa.length-1]=='gif'||aa[aa.length-1]=='jpg'||aa[aa.length-1]=='bmp')//判断图片格式{alert('图片格式正确!')
return true}else{alert('对不起,你选择的图片格式不对\n图片格式应为*.jpg、*.gif、*.bmp')
直接上代码:var img = "imgurl"//imgurl 就是你的图片路径
function getBase64Image(img) {
var canvas = document.createElement("canvas")
canvas.width = img.width
canvas.height = img.height
var ctx = canvas.getContext("2d")
ctx.drawImage(img, 0, 0, img.width, img.height)
var ext = img.src.substring(img.src.lastIndexOf(".")+1).toLowerCase()
var dataURL = canvas.toDataURL("image/"+ext)
return dataURL
}
var image = new Image()
image.src = img
image.onload = function(){
var base64 = getBase64Image(image)
console.log(base64)
}
下面一小段代码给大家分享Nodejs把接收图片base64格式保存为文件存储到服务器上的方法,具体代码如下所示: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("保存成功!")
}
})
})