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 {String} csvCols - use '*' to override grid coluns
28 * @cfg {String} csvTitles - use '*' to override grid coluns
32 * @cfg {Function} success (optional) MAY fire on download completed (fails on attachments)..
33 * @cfg {Number} timeout (optional) in milliseconds before it gives up (default 30000 = 30s)
34 * @cfg {Roo.grid.Grid} grid (optional) if you want to just download a grid, (without renderers..)
38 Pman.Download = function(cfg)
52 if (this.newWindow && this.method == 'GET') {
53 // as ie seems buggy...
54 window.open( this.url + '?' + Roo.urlEncode(this.params || {}), '_blank');
62 this.createCsvFrame();
66 Roo.EventManager.on( this.csvFrame, 'load', this.onLoad, this);
70 this.method = this.method || 'GET';
72 if (this.method == 'GET' && !this.params) {
75 this.csvFrame.src = cfg.url;
76 //this.cleanup.defer(cfg.timeout || 30000,this);
84 Roo.log("creating form?");
86 this.form = new FormData();
88 var b = Roo.get(document.body);
89 this.form = b.createChild({
93 target : this.newWindow ? '_new' : this.csvFrame.id,
94 enctype : 'multipart/form-data'
98 // if(this.doctype == 'pdf'){
99 // this.pdfEmbed = b.createChild({
102 // pluginspage : 'http://www.adobe.com/products/acrobat/readstep2.html',
107 Roo.log(this.params);
108 for(var i in this.params) {
109 this.form.append(i, this.params[i]);
111 var el = this.form.createChild( {
117 value : this.params[i]
123 var req = new XMLHttpRequest();
124 req.open(this.method, this.url);
127 req.onload = function( ev )
129 if (req.status == 200) {
130 _t.success ? _t.sucess() : '';
132 _t.failure ? _t.failure() : '';
141 this.form.dom.submit();
142 this.cleanup.defer(this.timeout || 30000,this);
149 Roo.apply(Pman.Download.prototype, {
152 * @type {HTMLIframe} the iframe to download into.
170 createCsvFrame: function()
173 document.body.removeChild(this.csvFrame);
177 this.csvFrame = document.createElement('iframe');
178 this.csvFrame.id = id;
179 this.csvFrame.name = id;
180 this.csvFrame.className = 'x-hidden';
182 this.csvFrame.src = Roo.SSL_SECURE_URL;
184 document.body.appendChild(this.csvFrame);
187 document.frames[id].name = id;
194 // requested++; // second request is real one..
195 // if (requested < 2) {
204 var frame = this.csvFrame;
208 frame.contentWindow.document :
209 (frame.contentDocument || window.frames[Pman.Download.csvFrame.id].document);
212 if(doc && doc.body && doc.body.innerHTML.length){
213 // alert(doc.body.innerHTML);
215 Roo.MessageBox.alert("Download Error", doc.body.innerHTML);
223 Roo.log(doc.body.innerHTML);
227 Roo.log(e.toString());
233 // we can not actually do anything with the frame... as it may actually still be downloading..
238 // this will never fire.. see
239 // 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
240 if (this.success && success) {
249 // private - clean up download elements.
252 /* Roo.log('cleanup?');
260 Roo.EventManager.removeListener(this.csvFrame, 'load', this.onLoad, this);
261 Roo.get(this.csvFrame).remove();
262 this.csvFrame= false;
268 buildFromGrid : function()
270 // get the params from beforeLoad
271 var ds = this.grid.ds;
272 ds.fireEvent('beforeload', ds, {
277 if(ds.sortInfo && ds.remoteSort){
278 var pn = ds.paramNames;
279 this.params[pn["sort"]] = ds.sortInfo.field;
280 this.params[pn["dir"]] = ds.sortInfo.direction;
283 var pn = ds.paramNames;
284 this.params[pn["multisort"]] = Roo.encode( { sort : ds.sortToggle, order: ds.sortOrder });
289 this.url = this.grid.ds.proxy.conn.url;
290 this.method = this.method || this.grid.ds.proxy.conn.method ;
295 t.params.csvCols = this.csvCols;
296 t.params.csvTitles = this.csvTitles;
299 Roo.each(this.grid.cm.config, function(c,i) {
300 t.params['csvCols['+i+']'] = c.dataIndex;
301 t.params['csvTitles['+i+']'] = c.header;
305 if (this.grid.loadMask) {
306 this.grid.loadMask.onLoad();
308 this.params.limit = this.limit;
310 // do it as a post, as args can get long..
312 this.method = this.method || 'POST';
313 if (this.method == 'POST') {
314 this.params._get = 1;