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)
42 // as ie seems buggy...
43 window.open( cfg.url + '?' + Roo.urlEncode(cfg.params || {}), '_blank');
51 this.createCsvFrame();
55 Roo.EventManager.on( this.csvFrame, 'load', this.onLoad, this);
59 this.method = this.method || 'GET';
61 if (this.method == 'GET' && !this.params) {
64 this.csvFrame.src = cfg.url;
65 this.cleanup.defer(cfg.timeout || 30000,this);
73 Roo.log("creating form?");
75 var b = Roo.get(document.body);
76 this.form = b.createChild({
80 target : this.csvFrame.id,
81 enctype : 'multipart/form-data'
87 for(var i in this.params) {
89 var el = this.form.createChild( {
95 value : this.params[i]
103 this.form.dom.submit();
104 this.cleanup.defer(this.timeout || 30000,this);
111 Roo.apply(Pman.Download.prototype, {
114 * @type {HTMLIframe} the iframe to download into.
123 createCsvFrame: function()
127 document.body.removeChild(this.csvFrame);
131 this.csvFrame = document.createElement('iframe');
132 this.csvFrame.id = id;
133 this.csvFrame.name = id;
134 this.csvFrame.className = 'x-hidden';
136 this.csvFrame.src = Roo.SSL_SECURE_URL;
138 document.body.appendChild(this.csvFrame);
141 document.frames[id].name = id;
148 // requested++; // second request is real one..
149 // if (requested < 2) {
157 var frame = this.csvFrame;
161 frame.contentWindow.document :
162 (frame.contentDocument || window.frames[Pman.Download.csvFrame.id].document);
165 if(doc && doc.body && doc.body.innerHTML.length){
166 // alert(doc.body.innerHTML);
168 Roo.MessageBox.alert("Download Error", doc.body.innerHTML);
174 Roo.log(doc.body.innerHTML);
178 Roo.log(e.toString());
184 // this will never fire.. see
185 // 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
186 if (this.success && success) {
195 // private - clean up download elements.
205 Roo.EventManager.removeListener(this.csvFrame, 'load', this.onLoad, this);
206 Roo.get(this.csvFrame).remove();
207 this.csvFrame= false;
212 buildFromGrid : function()
214 // get the params from beforeLoad
215 this.grid.ds.fireEvent('beforeload', {
219 this.url = this.grid.ds.proxy.conn.url;
220 this.method = this.grid.ds.proxy.conn.method ;
223 Roo.each(this.grid.cm.config, function(c,i) {
224 params['csvCols['+i+']'] = c.dataIndex;
225 params['csvTitles['+i+']'] = c.header;