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',
12 success : function() {
13 Roo.MessageBox.alert("File has downloaded");
20 * @param {Object} cfg Configuration object.
21 * @cfg {String} url Location to download from.
22 * @cfg {String} method GET or POST (default GET), POST will create a form, and post that into the hidden frame.
23 * @cfg {Boolean} newWindow (optional) download to new window
24 * @cfg {String} doctype (optional) download PDF to new window
25 * @cfg {Boolean limit (optional) limit for grid downloads.
27 * @cfg {Function} success (optional) MAY fire on download completed (fails on attachments)..
28 * @cfg {Number} timeout (optional) in milliseconds before it gives up (default 30000 = 30s)
29 * @cfg {Roo.grid.Grid} grid (optional) if you want to just download a grid, (without renderers..)
33 Pman.Download = function(cfg)
47 if (this.newWindow && this.method == 'GET') {
48 // as ie seems buggy...
49 window.open( cfg.url + '?' + Roo.urlEncode(cfg.params || {}), '_blank');
57 this.createCsvFrame();
61 Roo.EventManager.on( this.csvFrame, 'load', this.onLoad, this);
65 this.method = this.method || 'GET';
67 if (this.method == 'GET' && !this.params) {
70 this.csvFrame.src = cfg.url;
71 //this.cleanup.defer(cfg.timeout || 30000,this);
79 Roo.log("creating form?");
81 var b = Roo.get(document.body);
82 this.form = b.createChild({
86 target : this.newWindow ? '_new' : this.csvFrame.id,
87 enctype : 'multipart/form-data'
90 // if(this.doctype == 'pdf'){
91 // this.pdfEmbed = b.createChild({
94 // pluginspage : 'http://www.adobe.com/products/acrobat/readstep2.html',
100 for(var i in this.params) {
102 var el = this.form.createChild( {
108 value : this.params[i]
116 this.form.dom.submit();
117 this.cleanup.defer(this.timeout || 30000,this);
124 Roo.apply(Pman.Download.prototype, {
127 * @type {HTMLIframe} the iframe to download into.
142 createCsvFrame: function()
145 document.body.removeChild(this.csvFrame);
149 this.csvFrame = document.createElement('iframe');
150 this.csvFrame.id = id;
151 this.csvFrame.name = id;
152 this.csvFrame.className = 'x-hidden';
154 this.csvFrame.src = Roo.SSL_SECURE_URL;
156 document.body.appendChild(this.csvFrame);
159 document.frames[id].name = id;
166 // requested++; // second request is real one..
167 // if (requested < 2) {
176 var frame = this.csvFrame;
180 frame.contentWindow.document :
181 (frame.contentDocument || window.frames[Pman.Download.csvFrame.id].document);
184 if(doc && doc.body && doc.body.innerHTML.length){
185 // alert(doc.body.innerHTML);
187 Roo.MessageBox.alert("Download Error", doc.body.innerHTML);
193 Roo.log(doc.body.innerHTML);
197 Roo.log(e.toString());
200 // we can not actually do anything with the frame... as it may actually still be downloading..
205 // this will never fire.. see
206 // 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
207 if (this.success && success) {
216 // private - clean up download elements.
227 Roo.EventManager.removeListener(this.csvFrame, 'load', this.onLoad, this);
228 Roo.get(this.csvFrame).remove();
229 this.csvFrame= false;
234 buildFromGrid : function()
236 // get the params from beforeLoad
237 var ds = this.grid.ds;
238 ds.fireEvent('beforeload', ds, {
243 if(ds.sortInfo && ds.remoteSort){
244 var pn = ds.paramNames;
245 this.params[pn["sort"]] = ds.sortInfo.field;
246 this.params[pn["dir"]] = ds.sortInfo.direction;
249 var pn = ds.paramNames;
250 this.params[pn["multisort"]] = Roo.encode( { sort : ds.sortToggle, order: ds.sortOrder });
255 this.url = this.grid.ds.proxy.conn.url;
256 this.method = this.grid.ds.proxy.conn.method ;
259 Roo.each(this.grid.cm.config, function(c,i) {
260 t.params['csvCols['+i+']'] = c.dataIndex;
261 t.params['csvTitles['+i+']'] = c.header;
265 if (this.grid.loadMask) {
266 this.grid.loadMask.onLoad();
268 this.params.limit = this.limit;
270 // do it as a post, as args can get long..
271 this.params._get = 1;
272 this.method = 'POST';