【问题标题】:extjs 4 : display image from Mysql blob valueextjs 4:显示来自 Mysql blob 值的图像
【发布时间】:2013-02-17 17:22:01
【问题描述】:
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 hex值),如何从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

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

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

【问题讨论】:

    标签: javascript ajax extjs extjs4


    【解决方案1】:

    需要添加模型方法(并使用我对你另一个问题的回答中的转换器):

    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(" ")));
    }
    

    jsfiddle 上的示例以及您的图像和我妻子的照片。

    【讨论】:

      【解决方案2】:

      假设你的 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.
      });
      

      【讨论】:

      • Ext.getCmp('displayimage').src 是 xtype : 'container' ,但仍然无法显示图像。为什么?
      • 如果你想传递原始图像数据以在网页中显示它,你必须使用常规的 html 标签或 Ext.img 组件(xtype 'image')及其“src”属性集。所以你应该添加这样一个组件,也许在你的'displayimage'容器中。你能展示这个页面的完整源代码吗?
      • 所以,我可以看到您的 displayimage 是一个 Ext.Img (xtype 'image') 组件,这很棒。您是否使用浏览器的“检查元素”选项检查了“win”窗口中呈现的 html 是什么?有 标签吗?它的 src 属性包含什么?控制台上有任何错误消息吗?顺便说一句,您是否按照我在回答中的内容进行操作?
      • 不!?!我没有任何 标签?因为它不是html页面。我不知道你在说什么 =.="
      • 如果不构建 Web UI,你用 ExtJS 做什么?您使用哪种浏览器来测试您的代码?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-31
      • 2014-03-19
      • 1970-01-01
      • 2014-11-03
      相关资源
      最近更新 更多