Node.js - 图片(base64)上传OSS

图片在线生成base64

http://tool.chinaz.com/tools/imgtobase/
router.post('/upimg', function(req, res) {
    var data = req.body.data;
    //console.log(data);
 	//data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAkGBwgHBgkIBwgKCgkLDRYPDQwMDRsUFRAWIB0iIiAdHx8kKDQsJCYxJx8fLT0tMTU3Ojo6Iys/RD84QzQ5Ojf/2wBDAQoKCg0MDRoPDxo3JR8lNzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzf/......
 	
    var str = data.split(",");
    var suffix = data.split(";")[0].split("/")[1];
    console.log("IMG-Suffix====>>",suffix)

    //if(suffix == 'jpeg' || suffix == 'jpg' || suffix == 'png') {
        var client = {
	        region: 'oss-cn-hangzhou', //申请OSS服务的区域
	        accessKeyId: 'xxxxx',
	        accessKeySecret: 'xxxxx',
	        bucket: 'xxxxx'
	    };
        //base64
        var buffer = Buffer.from(str[1], 'base64');
        var objectKey = `guali/${new Date().getTime()}.` + suffix;

        client.put(objectKey, buffer).then(function (result) {
            res.send("上传图片成功", result.url)
        }).catch(function (err) {
            console.log(err);
            res.send(onfig.FileUploadError, "上传照片失败,请稍后重试!");
        });
    // } else {
    //     res.send(utils.failRes(config.ImgSuffixError, "照片格式错误,请稍后重试!"));
    // };
});

你可能感兴趣的:(Node.js)