ExtJS 4并显示来自BLOB的PDF

时间:2017-12-12 12:12:56

标签: pdf extjs4

我尝试在单独的窗口中显示PDF流,此流来自数据库BLOB字段。我的代码如下:

Ext.define('MyApp.view.ViewPDF', {
    extend: 'Ext.window.Window',
    alias: 'widget.mywindow',

    requires: [
        'Ext.toolbar.Toolbar',
        'Ext.button.Button'
    ],

    config: {
        title: '',
        source: ''
    },

    itemId:    'SHOW_PDF',
    closable:  false,
    resizable: true,
    modal:     true,

    onClose: function (clsbtn) {
        clsbtn.up('window').destroy();
    },


    initComponent: function () {
        var my = this;

        Ext.apply(my, {
            items: [
                {
                    xtype:  'panel',
                    layout: 'fit',
                    width:  640,
                    height: 780,
                    items: [
                        {
                            items: {
                                xtype:  'component',
                                align:  'stretch',
                                autoEl: {
                                    tag:      'iframe',
                                    loadMask: 'Creating report...please wait!',
                                    style:    'height: 100%; width: 100%; border: none',
                                    src:      'data:application/pdf;base64,' + tutaj.getSource()
                                }
                            }
                        }
                    ]
                }
            ],
            dockedItems: [
                {
                            xtype:  'toolbar',
                            dock:   'bottom',
                            height: 30,
                            items: [
                                '->',
                                {
                                    xtype:   'button',
                                    baseCls: 'x-btn-default-large',
                                    cls:     'cap-btn',
                                    handler: my.onClose,
                                    width:   55,
                                    margin:  '0, 10, 0, 0',
                                    style:   'text-align: center',
                                    text:    'Close'
                                }
                            ]
                        }
                    ]
        });
        my.callParent();
        my.title = my.getTitle();
    }
});

它只能通过FireFox浏览器工作。在Chrome中我可以看到空窗口。所以有两个问题(不能自己回答):

  1. 如何更正以上内容以在其他浏览器中显示此PDF流

  2. 如何在掩码中显示文本,因为上面代码中的loadMask不能 工作;只显示从窗口打开开始的文本,并在显示PDF时完成

  3. 非常友好地告诉我这段代码中的错误。

1 个答案:

答案 0 :(得分:1)

我使用filefieldBLOBFileReader创建了 FIDDLE 。我已经在chromeFire Fox进行了测试。我希望FIDDLE会帮助你或指导你解决问题。

CODE SNIPPET

Ext.create('Ext.form.Panel', {
    renderTo: Ext.getBody(),
    height: window.innerHeight,
    title: 'Iframe Example for PDF',
    bodyPadding: 10,

    items: [{
        xtype: 'fileuploadfield',
        buttonOnly: true,
        buttonText: 'Choose PDF and show via BLOB',
        listeners: {
            afterrender: function (cmp) {
                cmp.fileInputEl.set({
                    accept: '.pdf'
                });
            },
            change: function (f) {
                var file = document.getElementById(f.fileInputEl.id).files[0];
                this.up('form').doSetPDF(file);
            }
        }
    }, {
        xtype: 'fileuploadfield',
        buttonOnly: true,
        buttonText: 'Choose PDF and show via BASE64 ',
        listeners: {
            afterrender: function (cmp) {
                cmp.fileInputEl.set({
                    accept: '.pdf'
                });
            },
            change: function (f) {
                var file = document.getElementById(f.fileInputEl.id).files[0];
                this.up('form').doSetViaBase64(file);
            }
        }
    }, {
        xtype: 'box',
        autoEl: {
            tag: 'iframe',
            loadMask: 'Creating report...please wait!',
            width: '100%',
            height: '100%'
        }
    }],

    //Show pdf file using BLOB and createObjectURL
    doSetPDF: function (file) {
        if (file) {
            var form = this,
                blob, file;

            if (Ext.isIE) {
                this.doSetViaBase64(file)
            } else {
                blob = new Blob([file], {
                        type: 'application/pdf'
                    }),
                    file = window.URL.createObjectURL(blob);

                form.getEl().query('iframe')[0].src = file;
            }
        }
    },
    //Show pdf file using BASE64
    doSetViaBase64: function (file) {
        var form = this,
            reader = new FileReader();

        reader.readAsDataURL(file);
        reader.onload = function () {
            form.getEl().query('iframe')[0].src = this.result;
        };
        reader.onerror = function (error) {
            console.log('Error: ', error);
        };
    }
});
相关问题