X-Git-Url: http://git.roojs.org/?p=Pman.Core;a=blobdiff_plain;f=Pman.Download.js;h=2f16b24689413da76ec062818932f5ab201c7308;hp=3057f14c86934cbcc2fa8a307ed7a825af131c6b;hb=443623f47935eb52acd65a3ff0d84eab1fd5a98b;hpb=ede4e7f878e99c511c83e5ece0a926da2fd36927 diff --git a/Pman.Download.js b/Pman.Download.js index 3057f14c..2f16b246 100644 --- a/Pman.Download.js +++ b/Pman.Download.js @@ -8,6 +8,7 @@ var t = new Pman.Download({ url: baseURL + '/Images/Download/0/myfile.jpg', newWindow : false, params: { .... }, + doctype: 'pdf' success : function() { Roo.MessageBox.alert("File has downloaded"); } @@ -20,6 +21,14 @@ var t = new Pman.Download({ * @cfg {String} url Location to download from. * @cfg {String} method GET or POST (default GET), POST will create a form, and post that into the hidden frame. * @cfg {Boolean} newWindow (optional) download to new window +* @cfg {String} doctype (optional) download PDF to new window +* @cfg {Boolean} limit (optional) limit for grid downloads. + + * @cfg {String} csvCols - use '*' to override grid coluns + * @cfg {String} csvTitles - use '*' to override grid coluns + + + * @cfg {Function} success (optional) MAY fire on download completed (fails on attachments).. * @cfg {Number} timeout (optional) in milliseconds before it gives up (default 30000 = 30s) * @cfg {Roo.grid.Grid} grid (optional) if you want to just download a grid, (without renderers..) @@ -29,6 +38,8 @@ var t = new Pman.Download({ Pman.Download = function(cfg) { + + this.params = {}; Roo.apply(this, cfg); @@ -40,21 +51,21 @@ Pman.Download = function(cfg) } - if (cfg.newWindow) { - // as ie seems buggy... - window.open( cfg.url + '?' + Roo.urlEncode(cfg.params || {}), '_blank'); + if (this.newWindow && this.method == 'GET') { + // as ie seems buggy... + window.open( this.url + '?' + Roo.urlEncode(this.params || {}), '_blank'); return ; } - var submit = false; - this.createCsvFrame(); + //this.submit = false; + //this.createCsvFrame(); - var requested = 0; + //var requested = 0; - Roo.EventManager.on( this.csvFrame, 'load', this.onLoad, this); + //Roo.EventManager.on( this.csvFrame, 'load', this.onLoad, this); //--- simple method.. @@ -62,9 +73,13 @@ Pman.Download = function(cfg) if (this.method == 'GET' && !this.params) { (function() { - submit = true; + + + this.createCsvFrame(); + //Roo.EventManager.on( this.csvFrame, 'load', this.onLoad, this); + this.submit = true; this.csvFrame.src = cfg.url; - this.cleanup.defer(cfg.timeout || 30000,this); + //this.cleanup.defer(cfg.timeout || 30000,this); }).defer(100, this); @@ -74,20 +89,32 @@ Pman.Download = function(cfg) Roo.log("creating form?"); + this.form = new FormData(); + /* var b = Roo.get(document.body); this.form = b.createChild({ tag: 'form', method : this.method, action : this.url, - target : this.csvFrame.id, + target : this.newWindow ? '_new' : this.csvFrame.id, enctype : 'multipart/form-data' - - - }); + **/ +// +// if(this.doctype == 'pdf'){ +// this.pdfEmbed = b.createChild({ +// tag: 'embed', +// src : this.url, +// pluginspage : 'http://www.adobe.com/products/acrobat/readstep2.html', +// alt: this.doctype +// }); +// } + //Roo.log(this.params); + this.params._get = 1; // always do a post request, with _get passed for(var i in this.params) { - + this.form.append(i, this.params[i]); + /* var el = this.form.createChild( { ns : 'html', tag : 'input', @@ -96,16 +123,55 @@ Pman.Download = function(cfg) name : i, value : this.params[i] }); + */ } + var req = new XMLHttpRequest(); + req.responseType = 'blob'; + req.open('POST', this.url); + + var _t = this; + req.onload = function( ev ) + { + if (req.status == 200) { + Roo.log(ev); + var cd = req.getResponseHeader('Content-Disposition'); + + var filename = ''; + var matches = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/.exec(cd); + if (matches != null && matches[1]) { + filename = matches[1].replace(/['"]/g, ''); + } + + var blob = new Blob([req.response], {type: req.responseType }); + + var a = document.createElement("a"); + a.style = "display: none"; + document.body.appendChild(a); + var url = window.URL.createObjectURL(blob); + a.href = url; + a.download = filename; + a.click(); + //release the reference to the file by revoking the Object URL + window.URL.revokeObjectURL(url); + + _t.success ? _t.success(ev) : ''; + } else { + _t.failure ? _t.failure(ev) : ''; + } + + } + + req.send(this.form); + /* (function() { - submit = true; + this.submit = true; this.form.dom.submit(); this.cleanup.defer(this.timeout || 30000,this); }).defer(100, this); - + */ } @@ -121,10 +187,19 @@ Roo.apply(Pman.Download.prototype, { // private form : false, + limit : 9999, + + newWindow : false, + + method : 'GET', + + success : false, + failure : false, + // private.. + //used by simple GET method. createCsvFrame: function() { - if (this.csvFrame) { document.body.removeChild(this.csvFrame); } @@ -144,17 +219,18 @@ Roo.apply(Pman.Download.prototype, { } }, - + /* not used as it didn't work.. onLoad : function() { // requested++; // second request is real one.. // if (requested < 2) { // return; //} // n + Roo.log('onload?'); if (!this.submit) { - return; + return false; } - + //return false; var frame = this.csvFrame; var success = true; @@ -169,8 +245,10 @@ Roo.apply(Pman.Download.prototype, { Roo.MessageBox.alert("Download Error", doc.body.innerHTML); success = false; - - + if (this.failure) { + this.failure(); + } + return true; } Roo.log(doc.body.innerHTML); @@ -180,7 +258,12 @@ Roo.apply(Pman.Download.prototype, { Roo.log(e.toString()); Roo.log(e); } - + if (this.success) { + this.success(); + } + // we can not actually do anything with the frame... as it may actually still be downloading.. + return true; + this.cleanup(); // this will never fire.. see @@ -189,14 +272,15 @@ Roo.apply(Pman.Download.prototype, { this.success(); } - + return false; }, - + */ // private - clean up download elements. cleanup :function() { + /* Roo.log('cleanup?'); if (this.form) { this.form.remove(); this.form= false; @@ -208,37 +292,58 @@ Roo.apply(Pman.Download.prototype, { Roo.get(this.csvFrame).remove(); this.csvFrame= false; } + */ }, buildFromGrid : function() { // get the params from beforeLoad - this.grid.ds.fireEvent('beforeload', this.grid.ds, { + var ds = this.grid.ds; + ds.fireEvent('beforeload', ds, { params : this.params }); + + if(ds.sortInfo && ds.remoteSort){ + var pn = ds.paramNames; + this.params[pn["sort"]] = ds.sortInfo.field; + this.params[pn["dir"]] = ds.sortInfo.direction; + } + if (ds.multiSort) { + var pn = ds.paramNames; + this.params[pn["multisort"]] = Roo.encode( { sort : ds.sortToggle, order: ds.sortOrder }); + } + + + this.url = this.grid.ds.proxy.conn.url; - this.method = this.grid.ds.proxy.conn.method ; + this.method = this.method || this.grid.ds.proxy.conn.method ; var t = this; // work out the cols - Roo.each(this.grid.cm.config, function(c,i) { - t.params['csvCols['+i+']'] = c.dataIndex; - t.params['csvTitles['+i+']'] = c.header; + + if (this.csvCols) { + t.params.csvCols = this.csvCols; + t.params.csvTitles = this.csvTitles; + } else { - }); + Roo.each(this.grid.cm.config, function(c,i) { + t.params['csvCols['+i+']'] = c.dataIndex; + t.params['csvTitles['+i+']'] = c.header; + + }); + } if (this.grid.loadMask) { this.grid.loadMask.onLoad(); } - this.params.limit = 9999; + this.params.limit = this.limit; + // do it as a post, as args can get long.. + this.method = 'POST'; + if (this.method == 'POST') { + this.params._get = 1; + } } - - - - - - });