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 {Boolean limit (optional) limit for grid downloads.
25 * @cfg {Function} success (optional) MAY fire on download completed (fails on attachments)..
26 * @cfg {Number} timeout (optional) in milliseconds before it gives up (default 30000 = 30s)
27 * @cfg {Roo.grid.Grid} grid (optional) if you want to just download a grid, (without renderers..)
31 Pman.Download = function(cfg)
46 // as ie seems buggy...
47 window.open( cfg.url + '?' + Roo.urlEncode(cfg.params || {}), '_blank');
55 this.createCsvFrame();
59 Roo.EventManager.on( this.csvFrame, 'load', this.onLoad, this);
63 this.method = this.method || 'GET';
65 if (this.method == 'GET' && !this.params) {
68 this.csvFrame.src = cfg.url;
69 //this.cleanup.defer(cfg.timeout || 30000,this);
77 Roo.log("creating form?");
79 var b = Roo.get(document.body);
80 this.form = b.createChild({
84 target : this.csvFrame.id,
85 enctype : 'multipart/form-data'
91 for(var i in this.params) {
93 var el = this.form.createChild( {
99 value : this.params[i]
107 this.form.dom.submit();
108 this.cleanup.defer(this.timeout || 30000,this);
115 Roo.apply(Pman.Download.prototype, {
118 * @type {HTMLIframe} the iframe to download into.
129 createCsvFrame: function()
133 document.body.removeChild(this.csvFrame);
137 this.csvFrame = document.createElement('iframe');
138 this.csvFrame.id = id;
139 this.csvFrame.name = id;
140 this.csvFrame.className = 'x-hidden';
142 this.csvFrame.src = Roo.SSL_SECURE_URL;
144 document.body.appendChild(this.csvFrame);
147 document.frames[id].name = id;
154 // requested++; // second request is real one..
155 // if (requested < 2) {
164 var frame = this.csvFrame;
168 frame.contentWindow.document :
169 (frame.contentDocument || window.frames[Pman.Download.csvFrame.id].document);
172 if(doc && doc.body && doc.body.innerHTML.length){
173 // alert(doc.body.innerHTML);
175 Roo.MessageBox.alert("Download Error", doc.body.innerHTML);
181 Roo.log(doc.body.innerHTML);
185 Roo.log(e.toString());
188 // we can not actually do anything with the frame... as it may actually still be downloading..
193 // this will never fire.. see
194 // 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
195 if (this.success && success) {
204 // private - clean up download elements.
215 Roo.EventManager.removeListener(this.csvFrame, 'load', this.onLoad, this);
216 Roo.get(this.csvFrame).remove();
217 this.csvFrame= false;
222 buildFromGrid : function()
224 // get the params from beforeLoad
225 var ds = this.grid.ds;
226 ds.fireEvent('beforeload', ds, {
231 if(ds.sortInfo && ds.remoteSort){
232 var pn = ds.paramNames;
233 this.params[pn["sort"]] = ds.sortInfo.field;
234 this.params[pn["dir"]] = ds.sortInfo.direction;
237 var pn = ds.paramNames;
238 this.params[pn["multisort"]] = Roo.encode( { sort : ds.sortToggle, order: ds.sortOrder });
243 this.url = this.grid.ds.proxy.conn.url;
244 this.method = this.grid.ds.proxy.conn.method ;
247 Roo.each(this.grid.cm.config, function(c,i) {
248 t.params['csvCols['+i+']'] = c.dataIndex;
249 t.params['csvTitles['+i+']'] = c.header;
253 if (this.grid.loadMask) {
254 this.grid.loadMask.onLoad();
256 this.params.limit = this.limit;