简体   繁体   English

NodeJS将Base64字符串转换为jpeg图像-NodeJS或mysql

[英]NodeJS converting Base64 String To jpeg image - NodeJS or mysql

I have stored jpeg image in mysql database in format longblob...now i read that longblob and would like to retrieve jpeg image to see how it looks..i get only base64 string so how to convert it in mysql or node js? 我已经将jpeg图像以longblob格式存储在mysql数据库中...现在我读了longblob并想检索jpeg图像以查看其外观..我仅获得base64字符串,因此如何在mysql或node js中将其转换?

Here is my base64 string that is read from database: 这是我从数据库读取的base64字符串:

https:// jsfiddle.net/ ej4c9pk3/

How to decode it to jpeg image so that i can see how it looks and then i can display it on web page. 如何将其解码为jpeg图像,以便我可以看到它的外观,然后可以在网页上显示它。

Image base64 text is on the link...so you could see it and try it to decode to jpeg. 图片base64文字在链接上...因此您可以看到它,然后尝试将其解码为jpeg。

I write function that convert Base64 String to Base64 Hex String and now it is correct converted to display as BLOB png image...but problem is when i open field in DB Browser for SQLite it writes this converted value as Text, and must be written as binary (so that the image will be recognized and displayed when i choose Image in DB Browser for SQLIte...so the question is how can i insert Base64 Hex String to SQLite3 Database as binary? 我编写了将Base64字符串转换为Base64十六进制字符串的函数,现在可以正确转换为BLOB png图像显示...但是问题是,当我在DB Browser中打开SQLite字段时,它将转换后的值写为Text,并且必须写入作为二进制文件(这样,当我在数据库浏览器中选择SQLIte的图像时,将识别并显示图像...所以问题是如何将Base64十六进制字符串作为二进制文件插入SQLite3数据库?

Here is my code for insert: 这是我的插入代码:

/* GET - channels_logo */
for (var i in rows) {
    /* WRITE - table channels_logo */
    db.prepare('INSERT INTO channels_logo (logo_id, logo_channel, logo_png) VALUES 
    ("'+rows[i].id+'", "'+rows[i].channel+'", "'+(base64toHex(new Buffer(rows[i].logo).toString()))+'")').run();
};

function base64toHex(base64) {
    /* DEFINE - variables */
    var raw = atob(base64);
    var HEX = '';

    for (i = 0; i < raw.length; i++) {
        var _hex = raw.charCodeAt(i).toString(16)

        HEX += (_hex.length==2?_hex:'0'+_hex);
    };
    return HEX.toUpperCase();
};

[![Using this insert code is inserted as Text:][1]][1]

    https://i.stack.imgur.com/iZ2A1.jpg

[![And needs to be binary inserted and now it looks ok (i just erase text and copy text inserted into binary and now it works)][1]][1]


  https://i.stack.imgur.com/ZzGTU.jpg

So i see that SQLite3 displays inserted Base64 Hex As Text not binary...what i need to write in insert statement to insert it as binary? 所以我看到SQLite3显示插入的Base64十六进制为文本而不是二进制...我需要在insert语句中写什么才能将其作为二进制插入?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM