3
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.
    });

我创建了一个列表视图,当列表视图上的选择更改时,将显示图像Ext.getCmp('displayimage')

nodes[0].get("PicData")是选择
的图像数据图像数据是blob值(都是JPEG十六进制值),如何从extjs显示图像?

更新

这是我的显示图像代码

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]
                    }]
            });

在我将代码更改为之后

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

Image corrupt or truncated

这是我从萤火虫得到的错误消息,但我可以确定我的二进制数据是正确的,因为我尝试使用 python 将其转换为 .jpeg 文件

这是来自数据库的 .jpeg 示例 blob 值(二进制字符串), http:
//pastebin.ca/raw/2314500

4

2 回答 2

1

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.

于 2013-02-17T09:22:01.787 回答
1

假设你的Ext.getCmp('displayimage')代表一个 Ext.Img 组件,你可以设置它的“src”属性来包含图像数据,但是

  • 您必须将其编码为 base64,而不是十六进制

  • 您必须添加一个前缀(例如data:image/jpeg;base64,如果图像是 Jpeg 图像),表明您将传递实际数据而不是常规 Url

所以你应该写这样的东西:

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.
});
于 2013-02-16T18:04:45.853 回答