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)
55 if (this.newWindow && this.method == 'GET') {
56 // as ie seems buggy...
57 window.open( this.url + '?' + Roo.urlEncode(this.params || {}), '_blank');
64 //this.submit = false;
65 //this.createCsvFrame();
69 //Roo.EventManager.on( this.csvFrame, 'load', this.onLoad, this);
73 this.method = this.method || 'GET';
75 if (this.method == 'GET' && !this.params) {
79 this.createCsvFrame();
80 //Roo.EventManager.on( this.csvFrame, 'load', this.onLoad, this);
82 this.csvFrame.src = cfg.url;
83 //this.cleanup.defer(cfg.timeout || 30000,this);
91 Roo.log("creating form?");
93 this.form = new FormData();
95 var b = Roo.get(document.body);
96 this.form = b.createChild({
100 target : this.newWindow ? '_new' : this.csvFrame.id,
101 enctype : 'multipart/form-data'
105 // if(this.doctype == 'pdf'){
106 // this.pdfEmbed = b.createChild({
109 // pluginspage : 'http://www.adobe.com/products/acrobat/readstep2.html',
114 Roo.log(this.params);
115 for(var i in this.params) {
116 this.form.append(i, this.params[i]);
118 var el = this.form.createChild( {
124 value : this.params[i]
130 var req = new XMLHttpRequest();
131 req.open(this.method, this.url);
134 req.onload = function( ev )
136 if (req.status == 200) {
137 _t.success ? _t.success(ev) : '';
139 _t.failure ? _t.failure(ev) : '';
148 this.form.dom.submit();
149 this.cleanup.defer(this.timeout || 30000,this);
156 Roo.apply(Pman.Download.prototype, {
159 * @type {HTMLIframe} the iframe to download into.
177 //used by simple GET method.
178 createCsvFrame: function()
181 document.body.removeChild(this.csvFrame);
185 this.csvFrame = document.createElement('iframe');
186 this.csvFrame.id = id;
187 this.csvFrame.name = id;
188 this.csvFrame.className = 'x-hidden';
190 this.csvFrame.src = Roo.SSL_SECURE_URL;
192 document.body.appendChild(this.csvFrame);
195 document.frames[id].name = id;
199 /* not used as it didn't work..
202 // requested++; // second request is real one..
203 // if (requested < 2) {
212 var frame = this.csvFrame;
216 frame.contentWindow.document :
217 (frame.contentDocument || window.frames[Pman.Download.csvFrame.id].document);
220 if(doc && doc.body && doc.body.innerHTML.length){
221 // alert(doc.body.innerHTML);
223 Roo.MessageBox.alert("Download Error", doc.body.innerHTML);
231 Roo.log(doc.body.innerHTML);
235 Roo.log(e.toString());
241 // we can not actually do anything with the frame... as it may actually still be downloading..
246 // this will never fire.. see
247 // 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
248 if (this.success && success) {
257 // private - clean up download elements.
260 /* Roo.log('cleanup?');
268 Roo.EventManager.removeListener(this.csvFrame, 'load', this.onLoad, this);
269 Roo.get(this.csvFrame).remove();
270 this.csvFrame= false;
276 buildFromGrid : function()
278 // get the params from beforeLoad
279 var ds = this.grid.ds;
280 ds.fireEvent('beforeload', ds, {
285 if(ds.sortInfo && ds.remoteSort){
286 var pn = ds.paramNames;
287 this.params[pn["sort"]] = ds.sortInfo.field;
288 this.params[pn["dir"]] = ds.sortInfo.direction;
291 var pn = ds.paramNames;
292 this.params[pn["multisort"]] = Roo.encode( { sort : ds.sortToggle, order: ds.sortOrder });
297 this.url = this.grid.ds.proxy.conn.url;
298 this.method = this.method || this.grid.ds.proxy.conn.method ;
303 t.params.csvCols = this.csvCols;
304 t.params.csvTitles = this.csvTitles;
307 Roo.each(this.grid.cm.config, function(c,i) {
308 t.params['csvCols['+i+']'] = c.dataIndex;
309 t.params['csvTitles['+i+']'] = c.header;
313 if (this.grid.loadMask) {
314 this.grid.loadMask.onLoad();
316 this.params.limit = this.limit;
318 // do it as a post, as args can get long..
320 this.method = this.method || 'POST';
321 if (this.method == 'POST') {
322 this.params._get = 1;