user2040602
user2040602

Reputation:

extjs 4 : display image from Mysql blob value

Ext.define('ImageModel', {
        extend: 'Ext.data.Model',
        fields: ['PicID', 'PicUploadedDateTime','PicData']
    });

    var ImgStore = Ext.create('Ext.data.JsonStore', {
        model: 'ImageModel',
        autoLoad: true,
        proxy: {
            type: 'ajax',
            url: 'get-image.php',
            baseParams: {  //here you can define params you want to be sent on each request from this store
                        mainid: 'value1',
                        startdate: 'value2',
                        enddate: 'value3'
                        },
            reader: {
                type: 'json',
                root: 'images'
            }
        }
    });

    var listView = Ext.create('Ext.grid.Panel', {
        region: 'west',
        id : 'imagelist',
        title: 'Select Image',
        width: 200,
        split: true,
        collapsible: true,
        floatable: false,
        title:'Select Image',
        renderTo: Ext.getBody(),
        store: ImgStore,
        multiSelect: true,
        viewConfig: {
            emptyText: 'No images to display'
        },

        columns: [
            {
            text: 'Date Time Uploaded',
            //xtype: 'datecolumn',
            //format: 'Y-m-d H:i:s',
            flex: 65,
            width: 100,
            dataIndex: 'PicUploadedDateTime'
        }]
    });

listView.on('selectionchange', function(view, nodes){
        Ext.getCmp('displayimage') = nodes[0].get("PicData") // display the image on here
        //when listview selected the image,will display the image at here.
    });

i have create a listview,when selectionchange on listview,will show the image on Ext.getCmp('displayimage') .

nodes[0].get("PicData") is the selected image data
the image data are blob value(all are the JPEG hex value),how to display the image from extjs?

UPDATE

this is my displayimage code

button.on('click', function(){
        if (!win) {
            win = Ext.create('widget.window', {
                title: 'View Image',
                closable: true,
                closeAction: 'hide',
                width: 600,
                minWidth: 350,
                height: 550,
                layout: {
                    type: 'border',
                    padding: 5
                        },
                items:[
                        listView, 
                    {                           
                    region: 'center',
                    //xtype: 'tabpanel',
                    minheight: 350,
                    items: [{
                        //title: 'Bogus Tab',
                        xtype : 'image',
                        id : 'displayimage',
                            }]
                    },
                    {
                    region: 'north',
                    margin : "5 0 5 0",
                    //xtype: 'tabpanel',
                    minheight: 350,
                    items: [dr]
                    }]
            });

after i change the code to

Ext.getCmp('displayimage').src = 'data:image/jpeg;base64,'+nodes[0].get("PicData") // 

Image corrupt or truncated

this is the error message i get from firebug,but i can sure that my binary data are correct,because i have tried to use python convert it to .jpeg file

this is an .jpeg example blob value(binary string) from a database,
http://pastebin.ca/raw/2314500

Upvotes: 3

Views: 3704

Answers (2)

Vlad
Vlad

Reputation: 3645

Need to adding in model methods (and use converter from my answer of you another question):

getImage: function() {
    return this.getBase64(this.get('PicData'));
},
getBase64: function(str) {
    return btoa(String.fromCharCode.apply(null, str.replace(/\r|\n/g, "").replace(/([\da-fA-F]{2}) ?/g, "0x$1 ").replace(/ +$/, "").split(" ")));
}

Example on jsfiddle with your image & my wife photo.

Upvotes: 1

mbarthelemy
mbarthelemy

Reputation: 12913

Assuming that your Ext.getCmp('displayimage') represents an Ext.Img component , you can set its "src" property to contain the image data, but

  • you must have it encoded as base64, not hex

  • you have to add a prefix ( for example data:image/jpeg;base64, if the image is a Jpeg one) indicating that you will pass actual data instead of a regular Url

So you should write something like:

listView.on('selectionchange', function(view, nodes){
    Ext.getCmp('displayimage').src = 'data:image/jpeg;base64,'+nodes[0].get("PicData") // display the image on here
    //when listview selected the image,will display the image at here.
});

Upvotes: 1

Related Questions