Image not displaying when "src" is binary data from database

5,413 views
Skip to first unread message

dave bradley

unread,
Jun 1, 2012, 1:59:33 PM6/1/12
to KnockoutJS
I have an image stored in the database and have verified that the
image is valid. Here's the code:

self.BinaryData = ko.observable(data.BinaryData);

<td><img data-bind="attr: { 'src': BinaryData }"/></td>

Tried this as well:

<td><img data-bind="attr: { src: BinaryData }"/></td>

I'm wondering if the data is being read from the DB as binary??

Alpine Scrambler

unread,
Jun 1, 2012, 3:27:16 PM6/1/12
to knock...@googlegroups.com
You have to use this syntax:  http://en.wikipedia.org/wiki/Data_URI_scheme 

dave bradley

unread,
Jun 3, 2012, 2:40:03 PM6/3/12
to knock...@googlegroups.com
What would the syntax be?
 
td><img data-bind="attr: { src: "data:image/jpeg;base64," BinaryData }"/></td>

dave bradley

unread,
Jun 3, 2012, 8:03:19 PM6/3/12
to KnockoutJS
I found some more info in older posts, so this is what I've got:

<td><img data-bind="attr: { src: 'data:image/jpeg;base64,' +
BinaryData() }"/></td>

But it's still not working - any suggestions?

Alpine Scrambler

unread,
Jun 5, 2012, 2:55:42 PM6/5/12
to knock...@googlegroups.com
Is BinaryData() a function that returns a string representation of your image bytes?

k...@kog-records.com

unread,
Feb 10, 2013, 5:19:03 PM2/10/13
to knock...@googlegroups.com
Did you solve this?

kog coder

unread,
Feb 11, 2013, 1:12:04 PM2/11/13
to knock...@googlegroups.com
My images still not showing up. I have a viewmodel like this:

var vm = {
    ...
    items: ko.observableArray(),
    ...
};

The items contains a byte[] type property named "Logo". I attach a property "LogoImage" to the items:

vm.items.subscribe(function () {
        var items = this.items();
        for (var i = 0, j = items.length; i < j; i++) {
            var item = items[i];
            var base64String = "data:image/png;base64," + item.Logo();
            item.LogoImage = ko.observable(base64String);
        }
    }, vm);

The binding looks like this:

<td data-bind="attr: { src: LogoImage }"></td>

But nothing is showing up??

If I do this:
<td data-bind="text: LogoImage"></td>

I get e.g. : data:image/png;base64,iVBORw0KGgoAAAA....

If i run the string on browser (as url in chrome) i get the image. But why is the binding not working?

Can anyone help?

Thanks,
kog

Pete Davis

unread,
Feb 11, 2013, 1:29:38 PM2/11/13
to knock...@googlegroups.com
You wrote:

<td data-bind="attr: { src: LogoImage }">

<td> doesn't have a src attribute. Did you mean img?

k...@kog-records.com

unread,
Feb 12, 2013, 12:56:46 PM2/12/13
to knock...@googlegroups.com
Ohh, I missed the forest for the trees!

Thank you Pete!
Reply all
Reply to author
Forward
0 new messages