如何将包含图像的Base64代码的字符串转换为Byte?

时间:2019-04-07 14:55:22

标签: node.js mongoose

我正在尝试将图像保存在猫鼬中,我的客户端将该图像转换为Base64字符串并将其发送到服务器,我想将该字符串保存为字节 因为该字符串的大小大于图像,并且我想提高性能

这是我的英雄收藏

var mongoose = require('mongoose');
var Heroes = mongoose.model('Heroes',{
    Name:{
        type: String,
        required: true,
        trim: true 
    },
    Heroes_Image:{
        type: String,
        required: true
    }
});

module.exports = {Heroes};

这是我的服务器端:

var {mongoose} = require('./db/mongoose');
var {Heroes} = require('./models/Heroes');
var net = require('net');
var db = mongoose.connection;
var clients = [];
net.createServer(function (socket) {
    // Identify this client
    socket.name = socket.remotePort 

    // Put this new client in the list
    clients.push(socket);

    console.log(socket.name + " joined the chat");

    socket.on('data', function (data) {
        var j = JSON.parse(data);
        db.collection('Heroes').insertOne({
            Name: j.Name,
            Heroes_Image: j.Heroes_Image
        });// here save Base64 string of client image and it waste memory
    })
});

1 个答案:

答案 0 :(得分:0)

您可以使用Blob将其另存为文件。