1 //<script type="text/javascript">
4 * Handles file downloads in a hidden frame, or new window.
7 var t = new Pman.Download({
8 url: baseURL + '/Images/Download/0/myfile.jpg',
11 success : function() {
12 Roo.MessageBox.alert("File has downloaded");
19 * @param {Object} cfg Configuration object.
20 * @cfg {String} url Location to download from.
21 * @cfg {String} method GET or POST (default GET), POST will create a form, and post that into the hidden frame.
22 * @cfg {Boolean} newWindow (optional) download to new window
23 * @cfg {Function} success (optional) MAY fire on download completed (fails on attachments)..
24 * @cfg {Number} timeout (optional) in milliseconds before it gives up (default 30000 = 30s)
25 * @cfg {Roo.grid.Grid} grid (optional) if you want to just download a grid, (without renderers..)
29 Pman.Download = function(cfg)
44 // as ie seems buggy...
45 window.open( cfg.url + '?' + Roo.urlEncode(cfg.params || {}), '_blank');
53 this.createCsvFrame();
57 Roo.EventManager.on( this.csvFrame, 'load', this.onLoad, this);
61 this.method = this.method || 'GET';
63 if (this.method == 'GET' && !this.params) {
66 this.csvFrame.src = cfg.url;
67 this.cleanup.defer(cfg.timeout || 30000,this);
75 Roo.log("creating form?");
77 var b = Roo.get(document.body);
78 this.form = b.createChild({
82 target : this.csvFrame.id,
83 enctype : 'multipart/form-data'
89 for(var i in this.params) {
91 var el = this.form.createChild( {
97 value : this.params[i]
105 this.form.dom.submit();
106 this.cleanup.defer(this.timeout || 30000,this);
113 Roo.apply(Pman.Download.prototype, {
116 * @type {HTMLIframe} the iframe to download into.
125 createCsvFrame: function()
129 document.body.removeChild(this.csvFrame);
133 this.csvFrame = document.createElement('iframe');
134 this.csvFrame.id = id;
135 this.csvFrame.name = id;
136 this.csvFrame.className = 'x-hidden';
138 this.csvFrame.src = Roo.SSL_SECURE_URL;
140 document.body.appendChild(this.csvFrame);
143 document.frames[id].name = id;
150 // requested++; // second request is real one..
151 // if (requested < 2) {
159 var frame = this.csvFrame;
163 frame.contentWindow.document :
164 (frame.contentDocument || window.frames[Pman.Download.csvFrame.id].document);
167 if(doc && doc.body && doc.body.innerHTML.length){
168 // alert(doc.body.innerHTML);
170 Roo.MessageBox.alert("Download Error", doc.body.innerHTML);
176 Roo.log(doc.body.innerHTML);
180 Roo.log(e.toString());
186 // this will never fire.. see
187 // http://www.atalasoft.com/cs/blogs/jake/archive/2009/08/18/events-to-expect-when-dynamically-loading-iframes-in-javascript-take-2-thanks-firefox-3-5.aspx
188 if (this.success && success) {
197 // private - clean up download elements.
207 Roo.EventManager.removeListener(this.csvFrame, 'load', this.onLoad, this);
208 Roo.get(this.csvFrame).remove();
209 this.csvFrame= false;
214 buildFromGrid : function()
216 // get the params from beforeLoad
217 this.grid.ds.fireEvent('beforeload', this.grid.ds, {
221 this.url = this.grid.ds.proxy.conn.url;
222 this.method = this.grid.ds.proxy.conn.method ;
225 Roo.each(this.grid.cm.config, function(c,i) {
226 t.params['csvCols['+i+']'] = c.dataIndex;
227 t.params['csvTitles['+i+']'] = c.header;
230 if (this.grid.loadMask) {
231 this.grid.loadMask.onLoad();
233 this.params.limit = 9999;