【问题标题】:Insert base64 string to blog mysql with node.js使用 node.js 将 base64 字符串插入博客 mysql
【发布时间】:2021-01-23 08:03:05
【问题描述】:

我想在 mysql 博客中插入一个 base64 字符串。为此,我使用下面的代码,但是当我运行代码时出现以下错误:

ER_DATA_TOO_LONG:第 1 行的“Immagine”列的数据太长

想象是base64图像(png或jpg)

Node.js 代码:

async function inserimentoTipologia(Nome, Immagine) {
    var ret = true;
    var imgCast = new Buffer.from(Immagine, "base64");
    checkValueLoad = await new Promise((resolve, reject) => {
        return db.con.query("Insert into Categoria(Nome,Immagine) values( ? , ? ); ", [Nome, imgCast], function(err, results) {
            if (err) {
                ManageError.SendError("Errore: nella funzione inserimentoTipologia " + err);
                ret = false;
            }
            resolve(true);
        });
    });
    return ret;
}

【问题讨论】:

  • 您没有插入 base64 字符串 - imgCastBufferImagine是什么类型的?
  • 同意@eol。 .您为Immagine 列设置了什么类型?第二次尝试这样:const imgCast = new Buffer.from(Immagine).toString('base64').
  • 想象是base64 png

标签: mysql node.js base64 blob


【解决方案1】:
    var ret = true;
    checkValueLoad = await new Promise((resolve, reject) => {
        return db.con.query("Insert into Categoria(Nome,Immagine) values( ? , ? ); ", [Nome, Buffer.from(Immagine).toString('base64')], function(err, results) {
            if (err) {
                ManageError.SendError("Errore: nella funzione inserimentoTipologia " + err);
                ret = false;
            }
            resolve(true);
        });
    });
    return ret;
}

【讨论】:

    猜你喜欢
    • 2018-05-04
    • 2013-04-17
    • 1970-01-01
    • 2021-09-02
    • 2011-07-05
    • 2014-07-14
    • 2016-06-30
    • 1970-01-01
    • 2018-02-06
    相关资源
    最近更新 更多