Pman.Download.js
[Pman.Core] / Pman.Download.js
1 //<script type="text/javascript">
2 /**
3 * @class Pman.Download
4 * Handles file downloads in a hidden frame, or new window.
5 * Usage:
6 <pre><code>
7 var t = new Pman.Download({
8     url: baseURL + '/Images/Download/0/myfile.jpg',
9     newWindow : false,
10     params: { .... },
11     success : function() {
12         Roo.MessageBox.alert("File has downloaded");
13     }
14 });
15
16 </code></pre>
17
18 * @constructor
19 * @param {Object} cfg   Configuration object.
20 * @cfg {String} url     Location to download from.
21 * @cfg {String} method     GET or POST (default GET), POST will create a form, and post that into the hidden frame.
22 * @cfg {Boolean} newWindow (optional) download to new window
23 * @cfg {Function} success (optional) MAY fire on download completed (fails on attachments)..
24 * @cfg {Number} timeout (optional) in milliseconds before it gives up (default 30000 = 30s)
25 * @cfg {Roo.grid.Grid} grid (optional) if you want to just download a grid, (without renderers..)
26
27 */
28
29 Pman.Download = function(cfg)
30 {
31  
32     
33     
34     if (cfg.newWindow) {
35             // as ie seems buggy...
36         window.open( cfg.url + '?' + Roo.urlEncode(cfg.params || {}), '_blank');
37         return ; 
38         
39     }
40     Roo.apply(this, cfg);
41     
42     var submit = false;
43     this.createCsvFrame();
44     
45     var requested = 0;
46      
47     Roo.EventManager.on( this.csvFrame, 'load', this.onLoad, this);
48     
49     
50     //--- simple method..
51     cfg.method = cfg.method || 'GET';
52     
53     if (cfg.method == 'GET' && !cfg.params) {
54         (function() {
55             submit = true;
56             this.csvFrame.src = cfg.url;
57             this.cleanup.defer(cfg.timeout || 30000,this);
58         }).defer(100, this);
59         
60        
61         return;
62     }
63     
64     
65     Roo.log("creating form?");
66     
67     var b = Roo.get(document.body);
68     this.form = b.createChild({
69         tag: 'form',
70         method : cfg.method,
71         action : cfg.url,
72         target : this.csvFrame.id,
73         enctype : 'multipart/form-data'
74
75
76         
77     });
78  
79     for(var i in cfg.params) {
80         
81         var el = this.form.createChild( {
82             ns : 'html',
83             tag : 'input',
84             
85             type: 'hidden',
86             name : i,
87             value : cfg.params[i]
88         });
89         
90         
91     }
92     
93     (function() {
94         submit = true;
95         this.form.dom.submit();
96         this.cleanup.defer(cfg.timeout || 30000,this);
97     }).defer(100, this);
98     
99      
100  
101 }
102
103 Roo.apply(Pman.Download.prototype, {
104     
105     /**
106      * @type {HTMLIframe} the iframe to download into.
107      */
108      
109     csvFrame : false,
110     
111     // private
112     form : false,
113     
114     // private..
115     createCsvFrame: function()
116     {
117         
118         if (this.csvFrame) {
119             document.body.removeChild(this.csvFrame);
120         }
121             
122         var id = Roo.id();
123         this.csvFrame = document.createElement('iframe');
124         this.csvFrame.id = id;
125         this.csvFrame.name = id;
126         this.csvFrame.className = 'x-hidden';
127         //if(Roo.isIE){
128             this.csvFrame.src = Roo.SSL_SECURE_URL;
129         //}
130         document.body.appendChild(this.csvFrame);
131
132         if(Roo.isIE){
133             document.frames[id].name = id;
134         }
135         
136     },
137     
138     onLoad : function()
139     {
140        // requested++; // second request is real one..
141        // if (requested < 2) {
142        //     return;
143         //} // n
144         if (!this.submit) {
145             return;
146         }
147         
148       
149         var frame = this.csvFrame;
150         var success  = true; 
151         try { 
152             var doc = Roo.isIE ? 
153                 frame.contentWindow.document : 
154                 (frame.contentDocument || window.frames[Pman.Download.csvFrame.id].document);
155             
156             
157             if(doc && doc.body && doc.body.innerHTML.length){
158               //  alert(doc.body.innerHTML);
159                   
160                 Roo.MessageBox.alert("Download Error", doc.body.innerHTML);
161                 success  = false;
162                  
163                 
164             }
165             
166             Roo.log(doc.body.innerHTML);
167              
168         }
169         catch(e) {
170             Roo.log(e.toString());
171             Roo.log(e);
172         }
173         
174         this.cleanup();
175         
176         // this will never fire.. see 
177         // 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
178         if (this.success && success) {
179             
180             this.success();
181         }
182        
183         
184
185     },
186     
187     // private - clean up download elements.
188     cleanup :function()
189     {
190         if (this.form) {
191             this.form.remove();
192             this.form= false;
193         
194         }
195         
196         if (this.csvFrame) {
197             Roo.EventManager.removeListener(this.csvFrame, 'load', this.onLoad, this);
198             Roo.get(this.csvFrame).remove();
199             this.csvFrame= false;
200         }
201          
202     }
203      
204      
205 });