简体   繁体   中英

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?

Here is my base64 string that is read from database:

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.

Image base64 text is on the link...so you could see it and try it to decode to 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?

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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