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');
61 //this.submit = false;
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) {
76 this.createCsvFrame();
77 //Roo.EventManager.on( this.csvFrame, 'load', this.onLoad, this);
79 this.csvFrame.src = cfg.url;
80 //this.cleanup.defer(cfg.timeout || 30000,this);
88 Roo.log("creating form?");
90 this.form = new FormData();
92 var b = Roo.get(document.body);
93 this.form = b.createChild({
97 target : this.newWindow ? '_new' : this.csvFrame.id,
98 enctype : 'multipart/form-data'
102 // if(this.doctype == 'pdf'){
103 // this.pdfEmbed = b.createChild({
106 // pluginspage : 'http://www.adobe.com/products/acrobat/readstep2.html',
111 Roo.log(this.params);
112 for(var i in this.params) {
113 this.form.append(i, this.params[i]);
115 var el = this.form.createChild( {
121 value : this.params[i]
127 var req = new XMLHttpRequest();
128 req.open(this.method, this.url);
131 req.onload = function( ev )
133 if (req.status == 200) {
134 _t.success ? _t.sucess() : '';
136 _t.failure ? _t.failure() : '';
145 this.form.dom.submit();
146 this.cleanup.defer(this.timeout || 30000,this);
153 Roo.apply(Pman.Download.prototype, {
156 * @type {HTMLIframe} the iframe to download into.
174 //used by simple GET method.
175 createCsvFrame: function()
178 document.body.removeChild(this.csvFrame);
182 this.csvFrame = document.createElement('iframe');
183 this.csvFrame.id = id;
184 this.csvFrame.name = id;
185 this.csvFrame.className = 'x-hidden';
187 this.csvFrame.src = Roo.SSL_SECURE_URL;
189 document.body.appendChild(this.csvFrame);
192 document.frames[id].name = id;
196 /* not used as it didn't work..
199 // requested++; // second request is real one..
200 // if (requested < 2) {
209 var frame = this.csvFrame;
213 frame.contentWindow.document :
214 (frame.contentDocument || window.frames[Pman.Download.csvFrame.id].document);
217 if(doc && doc.body && doc.body.innerHTML.length){
218 // alert(doc.body.innerHTML);
220 Roo.MessageBox.alert("Download Error", doc.body.innerHTML);
228 Roo.log(doc.body.innerHTML);
232 Roo.log(e.toString());
238 // we can not actually do anything with the frame... as it may actually still be downloading..
243 // this will never fire.. see
244 // 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
245 if (this.success && success) {
254 // private - clean up download elements.
257 /* Roo.log('cleanup?');
265 Roo.EventManager.removeListener(this.csvFrame, 'load', this.onLoad, this);
266 Roo.get(this.csvFrame).remove();
267 this.csvFrame= false;
273 buildFromGrid : function()
275 // get the params from beforeLoad
276 var ds = this.grid.ds;
277 ds.fireEvent('beforeload', ds, {
282 if(ds.sortInfo && ds.remoteSort){
283 var pn = ds.paramNames;
284 this.params[pn["sort"]] = ds.sortInfo.field;
285 this.params[pn["dir"]] = ds.sortInfo.direction;
288 var pn = ds.paramNames;
289 this.params[pn["multisort"]] = Roo.encode( { sort : ds.sortToggle, order: ds.sortOrder });
294 this.url = this.grid.ds.proxy.conn.url;
295 this.method = this.method || this.grid.ds.proxy.conn.method ;
300 t.params.csvCols = this.csvCols;
301 t.params.csvTitles = this.csvTitles;
304 Roo.each(this.grid.cm.config, function(c,i) {
305 t.params['csvCols['+i+']'] = c.dataIndex;
306 t.params['csvTitles['+i+']'] = c.header;
310 if (this.grid.loadMask) {
311 this.grid.loadMask.onLoad();
313 this.params.limit = this.limit;
315 // do it as a post, as args can get long..
317 this.method = this.method || 'POST';
318 if (this.method == 'POST') {
319 this.params._get = 1;