将图片上传到mysql-Blob

时间:2020-08-13 10:19:44

标签: javascript mysql node.js typescript blob

我正在将前端的图像上传到mySql数据库中。

这是我的模特

const CheckIn = function (file) {
    this.type = file.type;
    this.name = file.name;
    this.data = file.data;
};

CheckIn.create = (newCheckIn, result) => {
    sql.query(`INSERT INTO files_checkin (type, name, data) VALUES (?,?,?)`,
        [newCheckIn.type, newCheckIn.name, newCheckIn.data], (err, res) => {
            if (err) {
                console.log("error: ", err);
                result(err, null);
                return;
            }
            console.log("created image: ", {
                nr: res.insertId,
                ...newCheckIn
            });
            result(null, {
                nr: res.insertId,
                ...newCheckIn
            });
        });

};

还有我的控制器的一部分

exports.create = (req, res) => {
    console.log("body " + JSON.stringify(req.body));
    if (!req.body) {
        res.status(400).send({
            message: "Content can not be empty!"
        });
    }

    const checkin = new CheckIn({
        type: req.file.mimetype,
        name: req.file.originalname,
        data: req.file.buffer
    });

    CheckIn.create(checkin, (err, data) => {

        if (err)
            res.status(500).send({
                message: err.message || "Some error occurred while creating the Alias."
            });
        else res.send(data);
    });
};

一切正常,直到我想上传例如5mb大小的图像为止。 然后我收到了loooooong blob,我的终端崩溃了。 看起来像矩阵。

我应该如何上传大块?

0 个答案:

没有答案
相关问题