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( cfg.url + '?' + Roo.urlEncode(cfg.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 var b = Roo.get(document.body);
87 this.form = b.createChild({
91 target : this.newWindow ? '_new' : this.csvFrame.id,
92 enctype : 'multipart/form-data'
95 // if(this.doctype == 'pdf'){
96 // this.pdfEmbed = b.createChild({
99 // pluginspage : 'http://www.adobe.com/products/acrobat/readstep2.html',
105 for(var i in this.params) {
107 var el = this.form.createChild( {
113 value : this.params[i]
121 this.form.dom.submit();
122 this.cleanup.defer(this.timeout || 30000,this);
129 Roo.apply(Pman.Download.prototype, {
132 * @type {HTMLIframe} the iframe to download into.
147 createCsvFrame: function()
150 document.body.removeChild(this.csvFrame);
154 this.csvFrame = document.createElement('iframe');
155 this.csvFrame.id = id;
156 this.csvFrame.name = id;
157 this.csvFrame.className = 'x-hidden';
159 this.csvFrame.src = Roo.SSL_SECURE_URL;
161 document.body.appendChild(this.csvFrame);
164 document.frames[id].name = id;
171 // requested++; // second request is real one..
172 // if (requested < 2) {
181 var frame = this.csvFrame;
185 frame.contentWindow.document :
186 (frame.contentDocument || window.frames[Pman.Download.csvFrame.id].document);
189 if(doc && doc.body && doc.body.innerHTML.length){
190 // alert(doc.body.innerHTML);
192 Roo.MessageBox.alert("Download Error", doc.body.innerHTML);
198 Roo.log(doc.body.innerHTML);
202 Roo.log(e.toString());
205 // we can not actually do anything with the frame... as it may actually still be downloading..
210 // this will never fire.. see
211 // 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
212 if (this.success && success) {
221 // private - clean up download elements.
232 Roo.EventManager.removeListener(this.csvFrame, 'load', this.onLoad, this);
233 Roo.get(this.csvFrame).remove();
234 this.csvFrame= false;
239 buildFromGrid : function()
241 // get the params from beforeLoad
242 var ds = this.grid.ds;
243 ds.fireEvent('beforeload', ds, {
248 if(ds.sortInfo && ds.remoteSort){
249 var pn = ds.paramNames;
250 this.params[pn["sort"]] = ds.sortInfo.field;
251 this.params[pn["dir"]] = ds.sortInfo.direction;
254 var pn = ds.paramNames;
255 this.params[pn["multisort"]] = Roo.encode( { sort : ds.sortToggle, order: ds.sortOrder });
260 this.url = this.grid.ds.proxy.conn.url;
261 this.method = this.grid.ds.proxy.conn.method ;
266 t.params.csvCols = this.csvCols;
267 t.params.csvTitles = this.csvTitles;
270 Roo.each(this.grid.cm.config, function(c,i) {
271 t.params['csvCols['+i+']'] = c.dataIndex;
272 t.params['csvTitles['+i+']'] = c.header;
276 if (this.grid.loadMask) {
277 this.grid.loadMask.onLoad();
279 this.params.limit = this.limit;
281 // do it as a post, as args can get long..
283 this.method = this.method || 'POST';
284 if (this.method == 'POST') {
285 this.params._get = 1;