eztam
14 Jul 2010, 12:29 AM
Hi,
I needed to open downloads in an iframe that a download will not be blocked by a popup blocker.
So I wrote a little class to load my download into an iframe:
Ext.ux.DownloadIframe = Ext.extend(Ext.util.Observable, function ()
{
var BEFOREDOWNLOADSTART = "beforedownloadstart",
DOWNLOADSTART = "downloadstart",
iframe = null,
loadMask = null;
return {
constructor: function (config)
{
if (!Ext.isEmpty(config))
{
Ext.apply(this, config, {
"name": "Ext_ux_DownloadIframe"
});
}
this.superclass().constructor.apply(this, arguments);
},
initComponent: function ()
{
this.superclass().initComponent.call(this);
this.addEvents(
BEFOREDOWNLOADSTART, DOWNLOADSTART);
},
start: function (download, loadMaskCfg)
{
if (iframe !== null) iframe.destroy();
var on =
{
};
on =
{
"fn": this.onBeforeDownloadStart,
"single": true,
"scope": this
};
on[DOWNLOADSTART] =
{
"fn": this.onDownloadStart,
"single": true,
"scope": this
};
this.on(on);
if (!Ext.isEmpty(loadMaskCfg))
{
if (loadMaskCfg instanceof Ext.Element)
{
loadMaskCfg =
{
"el": loadMaskCfg
};
}
var lmCfg =
{
"msg": "Loading..."
},
el = loadMaskCfg.el || Ext.getBody();
Ext.apply(lmCfg, loadMaskCfg);
loadMask = new Ext.LoadMask(el, lmCfg);
}
iframe = new Ext.BoxComponent(
{
"width": 0,
"height": 0,
"autoEl": {
"tag": "iframe",
"name": this.name,
"frameBorder": 0,
"src": download,
"style": {
"border": "0 none"
}
}
});
this.fireEvent(BEFOREDOWNLOADSTART, this, iframe, download);
iframe.render(this.renderTo || Ext.getBody());
iframe.el.dom.onload = this.onload.createDelegate(this, [this, iframe, download]);
},
onload: function (o, iframe, download)
{
o.fireEvent(DOWNLOADSTART, o, iframe, download);
},
onBeforeDownloadStart: function (o, iframe, download)
{
if (loadMask !== null) loadMask.show();
},
onDownloadStart: function (o, iframe, download)
{
if (loadMask !== null) loadMask.hide();
}
}
}());
Sample usage:
[B]var download = new Ext.ux.DownloadIframe();
var downloadBtn = new Ext.Button({
"text": "download",
"handler": function() {
download.start("url/to/downloadfile.zip", true);
}
});
btn.render("content");
-> On button click the download will open and a loadMask (on body) will appear till the download starts.
Greets eztam!
I needed to open downloads in an iframe that a download will not be blocked by a popup blocker.
So I wrote a little class to load my download into an iframe:
Ext.ux.DownloadIframe = Ext.extend(Ext.util.Observable, function ()
{
var BEFOREDOWNLOADSTART = "beforedownloadstart",
DOWNLOADSTART = "downloadstart",
iframe = null,
loadMask = null;
return {
constructor: function (config)
{
if (!Ext.isEmpty(config))
{
Ext.apply(this, config, {
"name": "Ext_ux_DownloadIframe"
});
}
this.superclass().constructor.apply(this, arguments);
},
initComponent: function ()
{
this.superclass().initComponent.call(this);
this.addEvents(
BEFOREDOWNLOADSTART, DOWNLOADSTART);
},
start: function (download, loadMaskCfg)
{
if (iframe !== null) iframe.destroy();
var on =
{
};
on =
{
"fn": this.onBeforeDownloadStart,
"single": true,
"scope": this
};
on[DOWNLOADSTART] =
{
"fn": this.onDownloadStart,
"single": true,
"scope": this
};
this.on(on);
if (!Ext.isEmpty(loadMaskCfg))
{
if (loadMaskCfg instanceof Ext.Element)
{
loadMaskCfg =
{
"el": loadMaskCfg
};
}
var lmCfg =
{
"msg": "Loading..."
},
el = loadMaskCfg.el || Ext.getBody();
Ext.apply(lmCfg, loadMaskCfg);
loadMask = new Ext.LoadMask(el, lmCfg);
}
iframe = new Ext.BoxComponent(
{
"width": 0,
"height": 0,
"autoEl": {
"tag": "iframe",
"name": this.name,
"frameBorder": 0,
"src": download,
"style": {
"border": "0 none"
}
}
});
this.fireEvent(BEFOREDOWNLOADSTART, this, iframe, download);
iframe.render(this.renderTo || Ext.getBody());
iframe.el.dom.onload = this.onload.createDelegate(this, [this, iframe, download]);
},
onload: function (o, iframe, download)
{
o.fireEvent(DOWNLOADSTART, o, iframe, download);
},
onBeforeDownloadStart: function (o, iframe, download)
{
if (loadMask !== null) loadMask.show();
},
onDownloadStart: function (o, iframe, download)
{
if (loadMask !== null) loadMask.hide();
}
}
}());
Sample usage:
[B]var download = new Ext.ux.DownloadIframe();
var downloadBtn = new Ext.Button({
"text": "download",
"handler": function() {
download.start("url/to/downloadfile.zip", true);
}
});
btn.render("content");
-> On button click the download will open and a loadMask (on body) will appear till the download starts.
Greets eztam!