1

I'm trying to store images in DB2 Database as BLOB contents. I used JS to convert the image content to base64.

 function loadImageFileAsURL()
{
var filesSelected = document.getElementById("inputFileToLoad").files;
if (filesSelected.length > 0)
{
    var fileToLoad = filesSelected[0];

    var fileReader = new FileReader();

    fileReader.onload = function(fileLoadedEvent) 
    {
        var textAreaFileContents = document.getElementById
        (
            "textAreaFileContents"
        );

        textAreaFileContents.innerHTML = fileLoadedEvent.target.result;
        var ImgContent = fileLoadedEvent.target.result;
        $("#IMAGE").attr("src",ImgContent);

    };

    fileReader.readAsDataURL(fileToLoad);
}
}

Now I need to convert this base64 content to binary and store them to my DB2 Database. Is there any way to do this in JavaScript?

And how to fetch this data from the database and display it on my mobile app using Adapters. ?

Gopi S
  • 523
  • 1
  • 5
  • 17
  • why do you convert to base64, just upload the file with FormData and read the file as stream in your server code. – wayne Mar 04 '14 at 06:57
  • There is no server Code in Worklight. Take a look at this http://public.dhe.ibm.com/software/mobile-solutions/worklight/docs/v610/05_01_Overview_of_client_technologies.pdf – Gopi S Mar 04 '14 at 07:51
  • you can write a custom javascript procedure in your adapter. where do you use the code show in your question? – wayne Mar 04 '14 at 08:38

1 Answers1

0

Why do you not simply store the image as encoded to base64 in database? I think this would be better in your scenario...

  1. You receive an image
  2. You use some library to handle the encoding to base64
    Review this question: Base64 encoding and decoding in client-side Javascript
  3. You store the image-now a string, in the database
  4. When you need to display the image in the app, fetch the contents and decode it (see step 2)
Community
  • 1
  • 1
Idan Adar
  • 44,156
  • 13
  • 50
  • 89