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      
25 */
26 Pman.Download = function(cfg)
27 {
28  
29     
30     
31     if (cfg.newWindow) {
32             // as ie seems buggy...
33         window.open( cfg.url + '?' + Roo.urlEncode(cfg.params || {}), '_blank');
34         return ; 
35         
36     }
37     Roo.apply(this, cfg);
38     
39     var submit = false;
40     this.createCsvFrame();
41     
42     var requested = 0;
43      
44     Roo.EventManager.on( this.csvFrame, 'load', this.onLoad, this);
45     
46     
47     //--- simple method..
48     cfg.method = cfg.method || 'GET';
49     
50     if (cfg.method == 'GET') {
51         (function() {
52             submit = true;
53             this.csvFrame.src = cfg.url;
54             this.cleanup.defer(30000,this);
55         }).defer(100, this);
56         
57        
58         return;
59     }
60     
61     Roo.log("creating form?");
62     
63     var b = Roo.get(document.body);
64     this.form = b.createChild({
65         tag: 'form',
66         method : 'POST',
67         action : cfg.url,
68         target : this.csvFrame.id,
69         enctype : 'multipart/form-data'
70
71
72         
73     });
74  
75     for(var i in cfg.params) {
76         
77         var el = this.form.createChild( {
78             ns : 'html',
79             tag : 'input',
80             
81             type: 'hidden',
82             name : i,
83             value : cfg.params[i]
84         });
85         
86         
87     }
88     
89     (function() {
90         submit = true;
91         this.form.dom.submit();
92         this.cleanup.defer(30000,this);
93     }).defer(100, this);
94     
95      
96  
97 }
98
99 Roo.apply(Pman.Download.prototype, {
100     
101     /**
102      * @type {HTMLIframe} the iframe to download into.
103      */
104      
105     csvFrame : false,
106     
107     // private
108     form : false,
109     
110     // private..
111     createCsvFrame: function()
112     {
113         
114         if (this.csvFrame) {
115             document.body.removeChild(this.csvFrame);
116         }
117             
118         var id = Roo.id();
119         this.csvFrame = document.createElement('iframe');
120         this.csvFrame.id = id;
121         this.csvFrame.name = id;
122         this.csvFrame.className = 'x-hidden';
123         //if(Roo.isIE){
124             this.csvFrame.src = Roo.SSL_SECURE_URL;
125         //}
126         document.body.appendChild(this.csvFrame);
127
128         if(Roo.isIE){
129             document.frames[id].name = id;
130         }
131         
132     },
133     
134     onLoad : function()
135     {
136        // requested++; // second request is real one..
137        // if (requested < 2) {
138        //     return;
139         //} // n
140         if (!this.submit) {
141             return;
142         }
143         
144       
145         var frame = this.csvFrame;
146         var success  = true; 
147         try { 
148             var doc = Roo.isIE ? 
149                 frame.contentWindow.document : 
150                 (frame.contentDocument || window.frames[Pman.Download.csvFrame.id].document);
151             
152             
153             if(doc && doc.body && doc.body.innerHTML.length){
154               //  alert(doc.body.innerHTML);
155                   
156                 Roo.MessageBox.alert("Download Error", doc.body.innerHTML);
157                 success  = false;
158                  
159                 
160             }
161             
162             Roo.log(doc.body.innerHTML);
163              
164         }
165         catch(e) {
166             Roo.log(e.toString());
167             Roo.log(e);
168         }
169         
170         this.cleanup();
171         
172         // this will never fire.. see 
173         // 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
174         if (this.success && success) {
175             
176             this.success();
177         }
178        
179         
180
181     }
182     
183     // private - clean up download elements.
184     cleanup :function()
185     {
186         if (this.form) {
187             this.form.remove();
188             this.form= false;
189         
190         }
191         
192         if (this.csvFrame) {
193             Roo.EventManager.removeListener(this.csvFrame, 'load', this.onLoad, this);
194             Roo.get(this.csvFrame).remove();
195             this.csvFrame= false;
196         }
197          
198     }
199      
200      
201 });