Pman.Download.js
authorAlan Knowles <alan@akbkhome.com>
Fri, 10 Sep 2010 05:00:55 +0000 (13:00 +0800)
committerAlan Knowles <alan@akbkhome.com>
Fri, 10 Sep 2010 05:00:55 +0000 (13:00 +0800)
Pman.Download.js

index 098ab85..86c3350 100644 (file)
@@ -1,8 +1,71 @@
 //<script type="text/javascript">
+/**
+* @class Pman.Download
+* Handles file downloads in a hidden frame, or new window.
+* Usage:
+<pre><code>
+var t = new Pman.Download({
+    url: baseURL + '/Images/Download/0/myfile.jpg',
+    newWindow : false,
+    params: { .... },
+    success : function() {
+        Roo.MessageBox.alert("File has downloaded");
+    }
+});
 
+</code></pre>
+* 
+* @constructor
+* @param {Object} cfg   Configuration object.
+* @cfg {String} url     Location to download from.
+* @cfg {Boolean} newWindow (optional) download to new window
+     
+*/
+Pman.Download = function(cfg)
+{
+    if (c.newWindow) {
+            // as ie seems buggy...
+        window.open( c.url + '?' + Roo.urlEncode(c.params || {}), '_blank');
+        return;
+        
+    }
+    
+    this.createCsvFrame();
+    function cb(){
+        var r = { responseText : "", responseXML : null };
 
-Pman.Download = {
+        var frame = this.csvFrame;
+
+        try { 
+            var doc = Roo.isIE ? 
+                frame.contentWindow.document : 
+                (frame.contentDocument || window.frames[Pman.Download.csvFrame.id].document);
+            
+            if(doc && doc.body && doc.body.innerHTML.length){
+              //  alert(doc.body.innerHTML);
+                Roo.MessageBox.alert("Error download",doc.body.innerHTML);
+            }
+             
+        }
+        catch(e) {
+        }
+
+        Roo.EventManager.removeListener(frame, 'load', cb, this);
+        if (cfg.success) {
+            cfg.success();
+        }
+
+    }
+    Roo.EventManager.on( this.csvFrame, 'load', cb, this);
+    this.csvFrame.src = c.url;
+}
+
+Roo.apply(Pman.Download.prototype, {
     
+    /**
+     * @type {HTMLIframe} the iframe to download into.
+     */
+     
     csvFrame : false,
     
     createCsvFrame: function()
@@ -28,41 +91,5 @@ Pman.Download = {
         
     },
      
-    
-    
-    download : function(c) {
-        
-        if (c.newWindow) {
-            // as ie seems buggy...
-            window.open( c.url + '?' + Roo.urlEncode(c.params || {}), '_blank');
-            return;
-            
-        }
-        
-        this.createCsvFrame();
-        function cb(){
-            var r = { responseText : "", responseXML : null };
-
-            var frame = this.csvFrame;
-
-            try { 
-                var doc = Roo.isIE ? 
-                    frame.contentWindow.document : 
-                    (frame.contentDocument || window.frames[this.csvFrame.id].document);
-                
-                if(doc && doc.body && doc.body.innerHTML.length){
-                  //  alert(doc.body.innerHTML);
-                    Roo.MessageBox.alert("Error download",doc.body.innerHTML);
-                }
-                 
-            }
-            catch(e) {
-            }
-
-            Roo.EventManager.removeListener(frame, 'load', cb, this);
-        }
-        Roo.EventManager.on( this.csvFrame, 'load', cb, this);
-        this.csvFrame.src = c.url;
-    },
-};
\ No newline at end of file
+     
+});
\ No newline at end of file