| 875 |
lars |
1 |
/*
|
|
|
2 |
Downloadify: Client Side File Creation
|
|
|
3 |
JavaScript + Flash Library
|
|
|
4 |
|
|
|
5 |
Version: 0.2
|
|
|
6 |
|
|
|
7 |
Copyright (c) 2009 Douglas C. Neiner
|
|
|
8 |
|
|
|
9 |
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
10 |
of this software and associated documentation files (the "Software"), to deal
|
|
|
11 |
in the Software without restriction, including without limitation the rights
|
|
|
12 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
13 |
copies of the Software, and to permit persons to whom the Software is
|
|
|
14 |
furnished to do so, subject to the following conditions:
|
|
|
15 |
|
|
|
16 |
The above copyright notice and this permission notice shall be included in
|
|
|
17 |
all copies or substantial portions of the Software.
|
|
|
18 |
|
|
|
19 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
20 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
21 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
22 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
23 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
24 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
25 |
THE SOFTWARE.
|
|
|
26 |
*/
|
|
|
27 |
package {
|
|
|
28 |
import flash.system.Security;
|
|
|
29 |
import flash.events.Event;
|
|
|
30 |
import flash.events.MouseEvent;
|
|
|
31 |
import flash.net.FileReference;
|
|
|
32 |
import flash.net.FileFilter;
|
|
|
33 |
import flash.net.URLRequest;
|
|
|
34 |
import flash.display.*;
|
|
|
35 |
import flash.utils.ByteArray;
|
|
|
36 |
import flash.external.ExternalInterface;
|
|
|
37 |
import com.dynamicflash.util.Base64;
|
|
|
38 |
|
|
|
39 |
[SWF(backgroundColor="#CCCCCC")]
|
|
|
40 |
[SWF(backgroundAlpha=0)]
|
|
|
41 |
public class Downloadify extends Sprite {
|
|
|
42 |
|
|
|
43 |
private var options:Object,
|
|
|
44 |
file:FileReference = new FileReference(),
|
|
|
45 |
queue_name:String = "",
|
|
|
46 |
|
|
|
47 |
_width:Number = 0,
|
|
|
48 |
_height:Number = 0,
|
|
|
49 |
|
|
|
50 |
enabled:Boolean = true,
|
|
|
51 |
over:Boolean = false,
|
|
|
52 |
down:Boolean = false,
|
|
|
53 |
|
|
|
54 |
buttonImage:String = "images/download.png",
|
|
|
55 |
|
|
|
56 |
button:Loader;
|
|
|
57 |
|
|
|
58 |
public function Downloadify() {
|
|
|
59 |
Security.allowDomain('*');
|
|
|
60 |
|
|
|
61 |
stage.align = StageAlign.TOP_LEFT;
|
|
|
62 |
stage.scaleMode = StageScaleMode.NO_SCALE;
|
|
|
63 |
|
|
|
64 |
options = this.root.loaderInfo.parameters;
|
|
|
65 |
|
|
|
66 |
queue_name = options.queue_name.toString();
|
|
|
67 |
|
|
|
68 |
_width = options.width;
|
|
|
69 |
_height = options.height;
|
|
|
70 |
|
|
|
71 |
if(options.downloadImage){
|
|
|
72 |
buttonImage = options.downloadImage;
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
setupDefaultButton();
|
|
|
76 |
addChild(button);
|
|
|
77 |
|
|
|
78 |
this.buttonMode = true;
|
|
|
79 |
|
|
|
80 |
this.addEventListener(MouseEvent.CLICK, onMouseClickEvent);
|
|
|
81 |
this.addEventListener(MouseEvent.ROLL_OVER , onMouseEnter);
|
|
|
82 |
this.addEventListener(MouseEvent.ROLL_OUT , onMouseLeave);
|
|
|
83 |
this.addEventListener(MouseEvent.MOUSE_DOWN , onMouseDown);
|
|
|
84 |
this.addEventListener(MouseEvent.MOUSE_UP , onMouseUp);
|
|
|
85 |
|
|
|
86 |
ExternalInterface.addCallback('setEnabled', setEnabled);
|
|
|
87 |
|
|
|
88 |
file.addEventListener(Event.COMPLETE, onSaveComplete);
|
|
|
89 |
file.addEventListener(Event.CANCEL, onSaveCancel);
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
private function setEnabled(isEnabled:Boolean):Boolean {
|
|
|
93 |
enabled = isEnabled;
|
|
|
94 |
if(enabled === true){
|
|
|
95 |
button.y = 0;
|
|
|
96 |
this.buttonMode = true;
|
|
|
97 |
} else {
|
|
|
98 |
button.y = (-3 * _height);
|
|
|
99 |
this.buttonMode = false;
|
|
|
100 |
}
|
|
|
101 |
return enabled;
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
private function setupDefaultButton():void {
|
|
|
105 |
button = new Loader();
|
|
|
106 |
var urlReq:URLRequest = new URLRequest(buttonImage);
|
|
|
107 |
button.load(urlReq);
|
|
|
108 |
button.x = 0;
|
|
|
109 |
button.y = 0;
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
|
|
|
113 |
|
|
|
114 |
protected function onMouseEnter(event:Event):void {
|
|
|
115 |
if(enabled === true){
|
|
|
116 |
if(down === false) button.y = (-1 * _height);
|
|
|
117 |
over = true;
|
|
|
118 |
}
|
|
|
119 |
}
|
|
|
120 |
protected function onMouseLeave(event:Event):void {
|
|
|
121 |
if(enabled === true){
|
|
|
122 |
if(down === false) button.y = 0;
|
|
|
123 |
over = false;
|
|
|
124 |
}
|
|
|
125 |
}
|
|
|
126 |
protected function onMouseDown(event:Event):void {
|
|
|
127 |
if(enabled === true){
|
|
|
128 |
button.y = button.y = (-2 * _height);
|
|
|
129 |
down = true;
|
|
|
130 |
}
|
|
|
131 |
}
|
|
|
132 |
protected function onMouseUp(event:Event):void {
|
|
|
133 |
if(enabled === true){
|
|
|
134 |
if(over === false){
|
|
|
135 |
button.y = 0;
|
|
|
136 |
} else {
|
|
|
137 |
button.y = (-1 * _height);
|
|
|
138 |
}
|
|
|
139 |
down = false;
|
|
|
140 |
}
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
protected function onMouseClickEvent(event:Event):void{
|
|
|
144 |
var theData:String = ExternalInterface.call('Downloadify.getTextForSave',queue_name),
|
|
|
145 |
filename:String = ExternalInterface.call('Downloadify.getFileNameForSave',queue_name),
|
|
|
146 |
dataType:String = ExternalInterface.call('Downloadify.getDataTypeForSave',queue_name);
|
|
|
147 |
|
|
|
148 |
if (dataType == "string" && theData != "") {
|
|
|
149 |
file.save(theData, filename);
|
|
|
150 |
} else if (dataType == "base64" && theData){
|
|
|
151 |
file.save(Base64.decodeToByteArray(theData), filename);
|
|
|
152 |
} else {
|
|
|
153 |
onSaveError();
|
|
|
154 |
}
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
protected function onSaveComplete(event:Event):void{
|
|
|
158 |
trace('Save Complete');
|
|
|
159 |
ExternalInterface.call('Downloadify.saveComplete',queue_name);
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
protected function onSaveCancel(event:Event):void{
|
|
|
163 |
trace('Save Cancel');
|
|
|
164 |
ExternalInterface.call('Downloadify.saveCancel',queue_name);
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
protected function onSaveError():void{
|
|
|
168 |
trace('Save Error');
|
|
|
169 |
ExternalInterface.call('Downloadify.saveError',queue_name);
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
}
|
|
|
173 |
}
|