1

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.

John
  • 1,521
  • 3
  • 15
  • 31
  • Possible duplicate of [Base64 encoding and decoding in client-side Javascript](https://stackoverflow.com/questions/2820249/base64-encoding-and-decoding-in-client-side-javascript) – pabrantes Sep 01 '17 at 10:26
  • Possible duplicate of [How to display Base64 images in HTML?](https://stackoverflow.com/questions/8499633/how-to-display-base64-images-in-html) – damd Sep 01 '17 at 10:47
  • I think it is not duplicate i need to read from mysql blob base64 text image and then save it to sqlite3 db so that it can image be displayed in sqlite3 db as image not text – John Sep 01 '17 at 10:49
  • @John: One or both of our links answer your question as far as I can tell, to be honest. To decode base64-encoded data, use `atob`. To display it on a web page, you don't need to decode it, you can use "data URIs". – damd Sep 01 '17 at 10:56
  • https://jsfiddle.net/ej4c9pk3/1/ – EMX Sep 01 '17 at 11:10

1 Answers1

0

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?

John
  • 1,521
  • 3
  • 15
  • 31