1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525
|
package {
import flash.display.BlendMode;
import flash.display.DisplayObjectContainer;
import flash.display.Loader;
import flash.display.Stage;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.net.FileReferenceList;
import flash.net.FileReference;
import flash.net.FileFilter;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;
import flash.events.*;
import flash.external.ExternalInterface;
import flash.system.Security;
import flash.text.AntiAliasType;
import flash.text.GridFitType;
import flash.text.StaticText;
import flash.text.StyleSheet;
import flash.text.TextDisplayMode;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.ui.Mouse;
import flash.utils.Timer;
import flash.xml.XMLNode;
import flash.xml.XMLNodeType;
import FileItem;
import ExternalCall;
public class SWFUpload extends Sprite {
// Cause SWFUpload to start as soon as the movie starts
public static function main():void
{
var SWFUpload:SWFUpload = new SWFUpload();
}
private const build_number:String = "SWFUPLOAD 2.2.0";
// State tracking variables
private var fileBrowserMany:FileReferenceList = new FileReferenceList();
private var fileBrowserOne:FileReference = null; // This isn't set because it can't be reused like the FileReferenceList. It gets setup in the SelectFile method
private var file_queue:Array = new Array(); // holds a list of all items that are to be uploaded.
private var current_file_item:FileItem = null; // the item that is currently being uploaded.
private var file_index:Array = new Array();
private var successful_uploads:Number = 0; // Tracks the uploads that have been completed
private var queue_errors:Number = 0; // Tracks files rejected during queueing
private var upload_errors:Number = 0; // Tracks files that fail upload
private var upload_cancelled:Number = 0; // Tracks number of cancelled files
private var queued_uploads:Number = 0; // Tracks the FileItems that are waiting to be uploaded.
private var valid_file_extensions:Array = new Array();// Holds the parsed valid extensions.
private var serverDataTimer:Timer = null;
private var assumeSuccessTimer:Timer = null;
private var restoreExtIntTimer:Timer;
private var hasCalledFlashReady:Boolean = false;
// Callbacks
private var flashReady_Callback:String;
private var fileDialogStart_Callback:String;
private var fileQueued_Callback:String;
private var fileQueueError_Callback:String;
private var fileDialogComplete_Callback:String;
private var uploadStart_Callback:String;
private var uploadProgress_Callback:String;
private var uploadError_Callback:String;
private var uploadSuccess_Callback:String;
private var uploadComplete_Callback:String;
private var debug_Callback:String;
private var testExternalInterface_Callback:String;
private var cleanUp_Callback:String;
// Values passed in from the HTML
private var movieName:String;
private var uploadURL:String;
private var filePostName:String;
private var uploadPostObject:Object;
private var fileTypes:String;
private var fileTypesDescription:String;
private var fileSizeLimit:Number;
private var fileUploadLimit:Number = 0;
private var fileQueueLimit:Number = 0;
private var useQueryString:Boolean = false;
private var requeueOnError:Boolean = false;
private var httpSuccess:Array = [];
private var assumeSuccessTimeout:Number = 0;
private var debugEnabled:Boolean;
private var buttonLoader:Loader;
private var buttonTextField:TextField;
private var buttonCursorSprite:Sprite;
private var buttonImageURL:String;
private var buttonWidth:Number;
private var buttonHeight:Number;
private var buttonText:String;
private var buttonTextStyle:String;
private var buttonTextTopPadding:Number;
private var buttonTextLeftPadding:Number;
private var buttonAction:Number;
private var buttonCursor:Number;
private var buttonStateOver:Boolean;
private var buttonStateMouseDown:Boolean;
private var buttonStateDisabled:Boolean;
// Error code "constants"
// Size check constants
private var SIZE_TOO_BIG:Number = 1;
private var SIZE_ZERO_BYTE:Number = -1;
private var SIZE_OK:Number = 0;
// Queue errors
private var ERROR_CODE_QUEUE_LIMIT_EXCEEDED:Number = -100;
private var ERROR_CODE_FILE_EXCEEDS_SIZE_LIMIT:Number = -110;
private var ERROR_CODE_ZERO_BYTE_FILE:Number = -120;
private var ERROR_CODE_INVALID_FILETYPE:Number = -130;
// Upload Errors
private var ERROR_CODE_HTTP_ERROR:Number = -200;
private var ERROR_CODE_MISSING_UPLOAD_URL:Number = -210;
private var ERROR_CODE_IO_ERROR:Number = -220;
private var ERROR_CODE_SECURITY_ERROR:Number = -230;
private var ERROR_CODE_UPLOAD_LIMIT_EXCEEDED:Number = -240;
private var ERROR_CODE_UPLOAD_FAILED:Number = -250;
private var ERROR_CODE_SPECIFIED_FILE_ID_NOT_FOUND:Number = -260;
private var ERROR_CODE_FILE_VALIDATION_FAILED:Number = -270;
private var ERROR_CODE_FILE_CANCELLED:Number = -280;
private var ERROR_CODE_UPLOAD_STOPPED:Number = -290;
// Button Actions
private var BUTTON_ACTION_SELECT_FILE:Number = -100;
private var BUTTON_ACTION_SELECT_FILES:Number = -110;
private var BUTTON_ACTION_START_UPLOAD:Number = -120;
private var BUTTON_CURSOR_ARROW:Number = -1;
private var BUTTON_CURSOR_HAND:Number = -2;
public function SWFUpload() {
// Do the feature detection. Make sure this version of Flash supports the features we need. If not
// abort initialization.
if (!flash.net.FileReferenceList || !flash.net.FileReference || !flash.net.URLRequest || !flash.external.ExternalInterface || !flash.external.ExternalInterface.available || !DataEvent.UPLOAD_COMPLETE_DATA) {
return;
}
// Keep Flash Player busy so it doesn't show the "flash script is running slowly" error
var counter:Number = 0;
root.addEventListener(Event.ENTER_FRAME, function ():void { if (++counter > 100) counter = 0; });
// Setup file FileReferenceList events
this.fileBrowserMany.addEventListener(Event.SELECT, this.Select_Many_Handler);
this.fileBrowserMany.addEventListener(Event.CANCEL, this.DialogCancelled_Handler);
this.stage.align = StageAlign.TOP_LEFT;
this.stage.scaleMode = StageScaleMode.NO_SCALE;
// Setup the button and text label
this.buttonLoader = new Loader();
var doNothing:Function = function ():void { };
this.buttonLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, doNothing );
this.buttonLoader.contentLoaderInfo.addEventListener(HTTPStatusEvent.HTTP_STATUS, doNothing );
this.stage.addChild(this.buttonLoader);
var self:SWFUpload = this;
this.stage.addEventListener(MouseEvent.CLICK, function (event:MouseEvent):void {
self.UpdateButtonState();
self.ButtonClickHandler(event);
});
this.stage.addEventListener(MouseEvent.MOUSE_DOWN, function (event:MouseEvent):void {
self.buttonStateMouseDown = true;
self.UpdateButtonState();
});
this.stage.addEventListener(MouseEvent.MOUSE_UP, function (event:MouseEvent):void {
self.buttonStateMouseDown = false;
self.UpdateButtonState();
});
this.stage.addEventListener(MouseEvent.MOUSE_OVER, function (event:MouseEvent):void {
self.buttonStateMouseDown = event.buttonDown;
self.buttonStateOver = true;
self.UpdateButtonState();
});
this.stage.addEventListener(MouseEvent.MOUSE_OUT, function (event:MouseEvent):void {
self.buttonStateMouseDown = false;
self.buttonStateOver = false;
self.UpdateButtonState();
});
// Handle the mouse leaving the flash movie altogether
this.stage.addEventListener(Event.MOUSE_LEAVE, function (event:Event):void {
self.buttonStateMouseDown = false;
self.buttonStateOver = false;
self.UpdateButtonState();
});
this.buttonTextField = new TextField();
this.buttonTextField.type = TextFieldType.DYNAMIC;
this.buttonTextField.antiAliasType = AntiAliasType.ADVANCED;
this.buttonTextField.autoSize = TextFieldAutoSize.NONE;
this.buttonTextField.cacheAsBitmap = true;
this.buttonTextField.multiline = true;
this.buttonTextField.wordWrap = false;
this.buttonTextField.tabEnabled = false;
this.buttonTextField.background = false;
this.buttonTextField.border = false;
this.buttonTextField.selectable = false;
this.buttonTextField.condenseWhite = true;
this.stage.addChild(this.buttonTextField);
this.buttonCursorSprite = new Sprite();
this.buttonCursorSprite.graphics.beginFill(0xFFFFFF, 0);
this.buttonCursorSprite.graphics.drawRect(0, 0, 1, 1);
this.buttonCursorSprite.graphics.endFill();
this.buttonCursorSprite.buttonMode = true;
this.buttonCursorSprite.x = 0;
this.buttonCursorSprite.y = 0;
this.buttonCursorSprite.addEventListener(MouseEvent.CLICK, doNothing);
this.stage.addChild(this.buttonCursorSprite);
// Get the movie name
this.movieName = root.loaderInfo.parameters.movieName || '';
this.movieName = this.movieName.replace(/[^a-zA-Z0-9\_\.\-]/g, "");
// **Configure the callbacks**
// The JavaScript tracks all the instances of SWFUpload on a page. We can access the instance
// associated with this SWF file using the movieName. Each callback is accessible by making
// a call directly to it on our instance. There is no error handling for undefined callback functions.
// A developer would have to deliberately remove the default functions,set the variable to null, or remove
// it from the init function.
this.flashReady_Callback = "SWFUpload.instances[\"" + this.movieName + "\"].flashReady";
this.fileDialogStart_Callback = "SWFUpload.instances[\"" + this.movieName + "\"].fileDialogStart";
this.fileQueued_Callback = "SWFUpload.instances[\"" + this.movieName + "\"].fileQueued";
this.fileQueueError_Callback = "SWFUpload.instances[\"" + this.movieName + "\"].fileQueueError";
this.fileDialogComplete_Callback = "SWFUpload.instances[\"" + this.movieName + "\"].fileDialogComplete";
this.uploadStart_Callback = "SWFUpload.instances[\"" + this.movieName + "\"].uploadStart";
this.uploadProgress_Callback = "SWFUpload.instances[\"" + this.movieName + "\"].uploadProgress";
this.uploadError_Callback = "SWFUpload.instances[\"" + this.movieName + "\"].uploadError";
this.uploadSuccess_Callback = "SWFUpload.instances[\"" + this.movieName + "\"].uploadSuccess";
this.uploadComplete_Callback = "SWFUpload.instances[\"" + this.movieName + "\"].uploadComplete";
this.debug_Callback = "SWFUpload.instances[\"" + this.movieName + "\"].debug";
this.testExternalInterface_Callback = "SWFUpload.instances[\"" + this.movieName + "\"].testExternalInterface";
this.cleanUp_Callback = "SWFUpload.instances[\"" + this.movieName + "\"].cleanUp";
// Get the Flash Vars
this.uploadURL = root.loaderInfo.parameters.uploadURL;
this.filePostName = root.loaderInfo.parameters.filePostName;
this.fileTypes = root.loaderInfo.parameters.fileTypes;
this.fileTypesDescription = root.loaderInfo.parameters.fileTypesDescription + " (" + this.fileTypes + ")";
this.loadPostParams(root.loaderInfo.parameters.params);
if (!this.filePostName) {
this.filePostName = "Filedata";
}
if (!this.fileTypes) {
this.fileTypes = "*.*";
}
if (!this.fileTypesDescription) {
this.fileTypesDescription = "All Files";
}
this.LoadFileExensions(this.fileTypes);
try {
this.debugEnabled = root.loaderInfo.parameters.debugEnabled == "true" ? true : false;
} catch (ex:Object) {
this.debugEnabled = false;
}
try {
this.SetFileSizeLimit(String(root.loaderInfo.parameters.fileSizeLimit));
} catch (ex:Object) {
this.fileSizeLimit = 0;
}
try {
this.fileUploadLimit = Number(root.loaderInfo.parameters.fileUploadLimit);
if (this.fileUploadLimit < 0) this.fileUploadLimit = 0;
} catch (ex:Object) {
this.fileUploadLimit = 0;
}
try {
this.fileQueueLimit = Number(root.loaderInfo.parameters.fileQueueLimit);
if (this.fileQueueLimit < 0) this.fileQueueLimit = 0;
} catch (ex:Object) {
this.fileQueueLimit = 0;
}
// Set the queue limit to match the upload limit when the queue limit is bigger than the upload limit
if (this.fileQueueLimit > this.fileUploadLimit && this.fileUploadLimit != 0) this.fileQueueLimit = this.fileUploadLimit;
// The the queue limit is unlimited and the upload limit is not then set the queue limit to the upload limit
if (this.fileQueueLimit == 0 && this.fileUploadLimit != 0) this.fileQueueLimit = this.fileUploadLimit;
try {
this.useQueryString = root.loaderInfo.parameters.useQueryString == "true" ? true : false;
} catch (ex:Object) {
this.useQueryString = false;
}
try {
this.requeueOnError = root.loaderInfo.parameters.requeueOnError == "true" ? true : false;
} catch (ex:Object) {
this.requeueOnError = false;
}
try {
this.SetHTTPSuccess(String(root.loaderInfo.parameters.httpSuccess));
} catch (ex:Object) {
this.SetHTTPSuccess([]);
}
try {
this.SetAssumeSuccessTimeout(Number(root.loaderInfo.parameters.assumeSuccessTimeout));
} catch (ex:Object) {
this.SetAssumeSuccessTimeout(0);
}
try {
this.SetButtonDimensions(Number(root.loaderInfo.parameters.buttonWidth), Number(root.loaderInfo.parameters.buttonHeight));
} catch (ex:Object) {
this.SetButtonDimensions(0, 0);
}
try {
this.SetButtonImageURL(String(root.loaderInfo.parameters.buttonImageURL));
} catch (ex:Object) {
this.SetButtonImageURL("");
}
try {
// HTML-escape strings that come from the user, preventing XSS.
this.SetButtonText(htmlEscape(String(root.loaderInfo.parameters.buttonText)));
} catch (ex:Object) {
this.SetButtonText("");
}
try {
this.SetButtonTextPadding(Number(root.loaderInfo.parameters.buttonTextLeftPadding), Number(root.loaderInfo.parameters.buttonTextTopPadding));
} catch (ex:Object) {
this.SetButtonTextPadding(0, 0);
}
try {
this.SetButtonTextStyle(String(root.loaderInfo.parameters.buttonTextStyle));
} catch (ex:Object) {
this.SetButtonTextStyle("");
}
try {
this.SetButtonAction(Number(root.loaderInfo.parameters.buttonAction));
} catch (ex:Object) {
this.SetButtonAction(this.BUTTON_ACTION_SELECT_FILES);
}
try {
this.SetButtonDisabled(root.loaderInfo.parameters.buttonDisabled == "true" ? true : false);
} catch (ex:Object) {
this.SetButtonDisabled(Boolean(false));
}
try {
this.SetButtonCursor(Number(root.loaderInfo.parameters.buttonCursor));
} catch (ex:Object) {
this.SetButtonCursor(this.BUTTON_CURSOR_ARROW);
}
this.SetupExternalInterface();
this.Debug("SWFUpload Init Complete");
this.PrintDebugInfo();
if (ExternalCall.Bool(this.testExternalInterface_Callback)) {
ExternalCall.Simple(this.flashReady_Callback);
this.hasCalledFlashReady = true;
}
// Start periodically checking the external interface
var oSelf:SWFUpload = this;
this.restoreExtIntTimer = new Timer(1000, 0);
this.restoreExtIntTimer.addEventListener(TimerEvent.TIMER, function ():void { oSelf.CheckExternalInterface();} );
this.restoreExtIntTimer.start();
}
// Used to periodically check that the External Interface functions are still working
private function CheckExternalInterface():void {
if (!ExternalCall.Bool(this.testExternalInterface_Callback)) {
this.SetupExternalInterface();
this.Debug("ExternalInterface reinitialized");
if (!this.hasCalledFlashReady) {
ExternalCall.Simple(this.flashReady_Callback);
this.hasCalledFlashReady = true;
}
}
}
// Called by JS to see if it can access the external interface
private function TestExternalInterface():Boolean {
return true;
}
private function SetupExternalInterface():void {
try {
ExternalInterface.addCallback("SelectFile", this.SelectFile);
ExternalInterface.addCallback("SelectFiles", this.SelectFiles);
ExternalInterface.addCallback("StartUpload", this.StartUpload);
ExternalInterface.addCallback("ReturnUploadStart", this.ReturnUploadStart);
ExternalInterface.addCallback("StopUpload", this.StopUpload);
ExternalInterface.addCallback("CancelUpload", this.CancelUpload);
ExternalInterface.addCallback("RequeueUpload", this.RequeueUpload);
ExternalInterface.addCallback("GetStats", this.GetStats);
ExternalInterface.addCallback("SetStats", this.SetStats);
ExternalInterface.addCallback("GetFile", this.GetFile);
ExternalInterface.addCallback("GetFileByIndex", this.GetFileByIndex);
ExternalInterface.addCallback("AddFileParam", this.AddFileParam);
ExternalInterface.addCallback("RemoveFileParam", this.RemoveFileParam);
ExternalInterface.addCallback("SetUploadURL", this.SetUploadURL);
ExternalInterface.addCallback("SetPostParams", this.SetPostParams);
ExternalInterface.addCallback("SetFileTypes", this.SetFileTypes);
ExternalInterface.addCallback("SetFileSizeLimit", this.SetFileSizeLimit);
ExternalInterface.addCallback("SetFileUploadLimit", this.SetFileUploadLimit);
ExternalInterface.addCallback("SetFileQueueLimit", this.SetFileQueueLimit);
ExternalInterface.addCallback("SetFilePostName", this.SetFilePostName);
ExternalInterface.addCallback("SetUseQueryString", this.SetUseQueryString);
ExternalInterface.addCallback("SetRequeueOnError", this.SetRequeueOnError);
ExternalInterface.addCallback("SetHTTPSuccess", this.SetHTTPSuccess);
ExternalInterface.addCallback("SetAssumeSuccessTimeout", this.SetAssumeSuccessTimeout);
ExternalInterface.addCallback("SetDebugEnabled", this.SetDebugEnabled);
ExternalInterface.addCallback("SetButtonImageURL", this.SetButtonImageURL);
ExternalInterface.addCallback("SetButtonDimensions", this.SetButtonDimensions);
ExternalInterface.addCallback("SetButtonText", this.SetButtonText);
ExternalInterface.addCallback("SetButtonTextPadding", this.SetButtonTextPadding);
ExternalInterface.addCallback("SetButtonTextStyle", this.SetButtonTextStyle);
ExternalInterface.addCallback("SetButtonAction", this.SetButtonAction);
ExternalInterface.addCallback("SetButtonDisabled", this.SetButtonDisabled);
ExternalInterface.addCallback("SetButtonCursor", this.SetButtonCursor);
ExternalInterface.addCallback("TestExternalInterface", this.TestExternalInterface);
} catch (ex:Error) {
this.Debug("Callbacks where not set: " + ex.message);
return;
}
ExternalCall.Simple(this.cleanUp_Callback);
}
/* *****************************************
* FileReference Event Handlers
* *************************************** */
private function DialogCancelled_Handler(event:Event):void {
this.Debug("Event: fileDialogComplete: File Dialog window cancelled.");
ExternalCall.FileDialogComplete(this.fileDialogComplete_Callback, 0, 0, this.queued_uploads);
}
private function Open_Handler(event:Event):void {
this.Debug("Event: uploadProgress (OPEN): File ID: " + this.current_file_item.id);
ExternalCall.UploadProgress(this.uploadProgress_Callback, this.current_file_item.ToJavaScriptObject(), 0, this.current_file_item.file_reference.size);
}
private function FileProgress_Handler(event:ProgressEvent):void {
// On early than Mac OS X 10.3 bytesLoaded is always -1, convert this to zero. Do bytesTotal for good measure.
// http://livedocs.adobe.com/flex/3/langref/flash/net/FileReference.html#event:progress
var bytesLoaded:Number = event.bytesLoaded < 0 ? 0 : event.bytesLoaded;
var bytesTotal:Number = event.bytesTotal < 0 ? 0 : event.bytesTotal;
// Because Flash never fires a complete event if the server doesn't respond after 30 seconds or on Macs if there
// is no content in the response we'll set a timer and assume that the upload is successful after the defined amount of
// time. If the timeout is zero then we won't use the timer.
if (bytesLoaded === bytesTotal && bytesTotal > 0 && this.assumeSuccessTimeout > 0) {
if (this.assumeSuccessTimer !== null) {
this.assumeSuccessTimer.stop();
this.assumeSuccessTimer = null;
}
this.assumeSuccessTimer = new Timer(this.assumeSuccessTimeout * 1000, 1);
this.assumeSuccessTimer.addEventListener(TimerEvent.TIMER_COMPLETE, AssumeSuccessTimer_Handler);
this.assumeSuccessTimer.start();
}
this.Debug("Event: uploadProgress: File ID: " + this.current_file_item.id + ". Bytes: " + bytesLoaded + ". Total: " + bytesTotal);
ExternalCall.UploadProgress(this.uploadProgress_Callback, this.current_file_item.ToJavaScriptObject(), bytesLoaded, bytesTotal);
}
private function AssumeSuccessTimer_Handler(event:TimerEvent):void {
this.Debug("Event: AssumeSuccess: " + this.assumeSuccessTimeout + " passed without server response");
this.UploadSuccess(this.current_file_item, "", false);
}
private function Complete_Handler(event:Event):void {
/* Because we can't do COMPLETE or DATA events (we have to do both) we can't
* just call uploadSuccess from the complete handler, we have to wait for
* the Data event which may never come. However, testing shows it always comes
* within a couple milliseconds if it is going to come so the solution is:
*
* Set a timer in the COMPLETE event (which always fires) and if DATA is fired
* it will stop the timer and call uploadComplete
*
* If the timer expires then DATA won't be fired and we call uploadComplete
* */
// Set the timer
if (serverDataTimer != null) {
this.serverDataTimer.stop();
this.serverDataTimer = null;
}
this.serverDataTimer = new Timer(100, 1);
//var self:SWFUpload = this;
this.serverDataTimer.addEventListener(TimerEvent.TIMER, this.ServerDataTimer_Handler);
this.serverDataTimer.start();
}
private function ServerDataTimer_Handler(event:TimerEvent):void {
this.UploadSuccess(this.current_file_item, "");
}
private function ServerData_Handler(event:DataEvent):void {
this.UploadSuccess(this.current_file_item, event.data);
}
private function UploadSuccess(file:FileItem, serverData:String, responseReceived:Boolean = true):void {
if (this.serverDataTimer !== null) {
this.serverDataTimer.stop();
this.serverDataTimer = null;
}
if (this.assumeSuccessTimer !== null) {
this.assumeSuccessTimer.stop();
this.assumeSuccessTimer = null;
}
this.successful_uploads++;
file.file_status = FileItem.FILE_STATUS_SUCCESS;
this.Debug("Event: uploadSuccess: File ID: " + file.id + " Response Received: " + responseReceived.toString() + " Data: " + serverData);
ExternalCall.UploadSuccess(this.uploadSuccess_Callback, file.ToJavaScriptObject(), serverData, responseReceived);
this.UploadComplete(false);
}
private function HTTPError_Handler(event:HTTPStatusEvent):void {
var isSuccessStatus:Boolean = false;
for (var i:Number = 0; i < this.httpSuccess.length; i++) {
if (this.httpSuccess[i] === event.status) {
isSuccessStatus = true;
break;
}
}
if (isSuccessStatus) {
this.Debug("Event: httpError: Translating status code " + event.status + " to uploadSuccess");
var serverDataEvent:DataEvent = new DataEvent(DataEvent.UPLOAD_COMPLETE_DATA, event.bubbles, event.cancelable, "");
this.ServerData_Handler(serverDataEvent);
} else {
this.upload_errors++;
this.current_file_item.file_status = FileItem.FILE_STATUS_ERROR;
this.Debug("Event: uploadError: HTTP ERROR : File ID: " + this.current_file_item.id + ". HTTP Status: " + event.status + ".");
ExternalCall.UploadError(this.uploadError_Callback, this.ERROR_CODE_HTTP_ERROR, this.current_file_item.ToJavaScriptObject(), event.status.toString());
this.UploadComplete(true); // An IO Error is also called so we don't want to complete the upload yet.
}
}
// Note: Flash Player does not support Uploads that require authentication. Attempting this will trigger an
// IO Error or it will prompt for a username and password and may crash the browser (FireFox/Opera)
private function IOError_Handler(event:IOErrorEvent):void {
// Only trigger an IO Error event if we haven't already done an HTTP error
if (this.current_file_item.file_status != FileItem.FILE_STATUS_ERROR) {
this.upload_errors++;
this.current_file_item.file_status = FileItem.FILE_STATUS_ERROR;
this.Debug("Event: uploadError : IO Error : File ID: " + this.current_file_item.id + ". IO Error: " + event.text);
ExternalCall.UploadError(this.uploadError_Callback, this.ERROR_CODE_IO_ERROR, this.current_file_item.ToJavaScriptObject(), event.text);
}
this.UploadComplete(true);
}
private function SecurityError_Handler(event:SecurityErrorEvent):void {
this.upload_errors++;
this.current_file_item.file_status = FileItem.FILE_STATUS_ERROR;
this.Debug("Event: uploadError : Security Error : File Number: " + this.current_file_item.id + ". Error text: " + event.text);
ExternalCall.UploadError(this.uploadError_Callback, this.ERROR_CODE_SECURITY_ERROR, this.current_file_item.ToJavaScriptObject(), event.text);
this.UploadComplete(true);
}
private function Select_Many_Handler(event:Event):void {
this.Select_Handler(this.fileBrowserMany.fileList);
}
private function Select_One_Handler(event:Event):void {
var fileArray:Array = new Array(1);
fileArray[0] = this.fileBrowserOne;
this.Select_Handler(fileArray);
}
private function Select_Handler(file_reference_list:Array):void {
this.Debug("Select Handler: Received the files selected from the dialog. Processing the file list...");
var num_files_queued:Number = 0;
// Determine how many queue slots are remaining (check the unlimited (0) settings, successful uploads and queued uploads)
var queue_slots_remaining:Number = 0;
if (this.fileUploadLimit == 0) {
queue_slots_remaining = this.fileQueueLimit == 0 ? file_reference_list.length : (this.fileQueueLimit - this.queued_uploads); // If unlimited queue make the allowed size match however many files were selected.
} else {
var remaining_uploads:Number = this.fileUploadLimit - this.successful_uploads - this.queued_uploads;
if (remaining_uploads < 0) remaining_uploads = 0;
if (this.fileQueueLimit == 0 || this.fileQueueLimit >= remaining_uploads) {
queue_slots_remaining = remaining_uploads;
} else if (this.fileQueueLimit < remaining_uploads) {
queue_slots_remaining = this.fileQueueLimit - this.queued_uploads;
}
}
if (queue_slots_remaining < 0) queue_slots_remaining = 0;
// Check if the number of files selected is greater than the number allowed to queue up.
if (queue_slots_remaining < file_reference_list.length) {
this.Debug("Event: fileQueueError : Selected Files (" + file_reference_list.length + ") exceeds remaining Queue size (" + queue_slots_remaining + ").");
ExternalCall.FileQueueError(this.fileQueueError_Callback, this.ERROR_CODE_QUEUE_LIMIT_EXCEEDED, null, queue_slots_remaining.toString());
} else {
// Process each selected file
for (var i:Number = 0; i < file_reference_list.length; i++) {
var file_item:FileItem = new FileItem(file_reference_list[i], this.movieName, this.file_index.length);
this.file_index[file_item.index] = file_item;
// Verify that the file is accessible. Zero byte files and possibly other conditions can cause a file to be inaccessible.
var jsFileObj:Object = file_item.ToJavaScriptObject();
var is_valid_file_reference:Boolean = (jsFileObj.filestatus !== FileItem.FILE_STATUS_ERROR);
if (is_valid_file_reference) {
// Check the size, if it's within the limit add it to the upload list.
var size_result:Number = this.CheckFileSize(file_item);
var is_valid_filetype:Boolean = this.CheckFileType(file_item);
if(size_result == this.SIZE_OK && is_valid_filetype) {
file_item.file_status = FileItem.FILE_STATUS_QUEUED;
this.file_queue.push(file_item);
this.queued_uploads++;
num_files_queued++;
this.Debug("Event: fileQueued : File ID: " + file_item.id);
ExternalCall.FileQueued(this.fileQueued_Callback, file_item.ToJavaScriptObject());
}
else if (!is_valid_filetype) {
file_item.file_reference = null; // Cleanup the object
this.queue_errors++;
this.Debug("Event: fileQueueError : File not of a valid type.");
ExternalCall.FileQueueError(this.fileQueueError_Callback, this.ERROR_CODE_INVALID_FILETYPE, file_item.ToJavaScriptObject(), "File is not an allowed file type.");
}
else if (size_result == this.SIZE_TOO_BIG) {
file_item.file_reference = null; // Cleanup the object
this.queue_errors++;
this.Debug("Event: fileQueueError : File exceeds size limit.");
ExternalCall.FileQueueError(this.fileQueueError_Callback, this.ERROR_CODE_FILE_EXCEEDS_SIZE_LIMIT, file_item.ToJavaScriptObject(), "File size exceeds allowed limit.");
}
else if (size_result == this.SIZE_ZERO_BYTE) {
file_item.file_reference = null; // Cleanup the object
this.queue_errors++;
this.Debug("Event: fileQueueError : File is zero bytes.");
ExternalCall.FileQueueError(this.fileQueueError_Callback, this.ERROR_CODE_ZERO_BYTE_FILE, file_item.ToJavaScriptObject(), "File is zero bytes and cannot be uploaded.");
}
} else {
file_item.file_reference = null; // Cleanup the object
this.queue_errors++;
this.Debug("Event: fileQueueError : File is zero bytes or FileReference is invalid.");
ExternalCall.FileQueueError(this.fileQueueError_Callback, this.ERROR_CODE_ZERO_BYTE_FILE, file_item.ToJavaScriptObject(), "File is zero bytes or cannot be accessed and cannot be uploaded.");
}
}
}
this.Debug("Event: fileDialogComplete : Finished processing selected files. Files selected: " + file_reference_list.length + ". Files Queued: " + num_files_queued);
ExternalCall.FileDialogComplete(this.fileDialogComplete_Callback, file_reference_list.length, num_files_queued, this.queued_uploads);
}
/* ****************************************************************
Externally exposed functions
****************************************************************** */
// Opens a file browser dialog that allows one file to be selected.
private function SelectFile():void {
this.fileBrowserOne = new FileReference();
this.fileBrowserOne.addEventListener(Event.SELECT, this.Select_One_Handler);
this.fileBrowserOne.addEventListener(Event.CANCEL, this.DialogCancelled_Handler);
// Default file type settings
var allowed_file_types:String = "*.*";
var allowed_file_types_description:String = "All Files";
// Get the instance settings
if (this.fileTypes.length > 0) allowed_file_types = this.fileTypes;
if (this.fileTypesDescription.length > 0) allowed_file_types_description = this.fileTypesDescription;
this.Debug("Event: fileDialogStart : Browsing files. Single Select. Allowed file types: " + allowed_file_types);
ExternalCall.Simple(this.fileDialogStart_Callback);
try {
this.fileBrowserOne.browse([new FileFilter(allowed_file_types_description, allowed_file_types)]);
} catch (ex:Error) {
this.Debug("Exception: " + ex.toString());
}
}
// Opens a file browser dialog that allows multiple files to be selected.
private function SelectFiles():void {
var allowed_file_types:String = "*.*";
var allowed_file_types_description:String = "All Files";
if (this.fileTypes.length > 0) allowed_file_types = this.fileTypes;
if (this.fileTypesDescription.length > 0) allowed_file_types_description = this.fileTypesDescription;
this.Debug("Event: fileDialogStart : Browsing files. Multi Select. Allowed file types: " + allowed_file_types);
ExternalCall.Simple(this.fileDialogStart_Callback);
try {
this.fileBrowserMany.browse([new FileFilter(allowed_file_types_description, allowed_file_types)]);
} catch (ex:Error) {
this.Debug("Exception: " + ex.toString());
}
}
// Cancel the current upload and stops. Doesn't advance the upload pointer. The current file is requeued at the beginning.
private function StopUpload():void {
if (this.current_file_item != null) {
// Cancel the upload and re-queue the FileItem
this.current_file_item.file_reference.cancel();
// Remove the event handlers
this.removeFileReferenceEventListeners(this.current_file_item);
this.current_file_item.file_status = FileItem.FILE_STATUS_QUEUED;
this.file_queue.unshift(this.current_file_item);
var js_object:Object = this.current_file_item.ToJavaScriptObject();
this.current_file_item = null;
this.Debug("Event: uploadError: upload stopped. File ID: " + js_object.ID);
ExternalCall.UploadError(this.uploadError_Callback, this.ERROR_CODE_UPLOAD_STOPPED, js_object, "Upload Stopped");
this.Debug("Event: uploadComplete. File ID: " + js_object.ID);
ExternalCall.UploadComplete(this.uploadComplete_Callback, js_object);
this.Debug("StopUpload(): upload stopped.");
} else {
this.Debug("StopUpload(): No file is currently uploading. Nothing to do.");
}
}
/* Cancels the upload specified by file_id
* If the file is currently uploading it is cancelled and the uploadComplete
* event gets called.
* If the file is not currently uploading then only the uploadCancelled event is fired.
* */
private function CancelUpload(file_id:String, triggerErrorEvent:Boolean = true):void {
var file_item:FileItem = null;
// Check the current file item
if (this.current_file_item != null && (this.current_file_item.id == file_id || !file_id)) {
this.current_file_item.file_reference.cancel();
this.current_file_item.file_status = FileItem.FILE_STATUS_CANCELLED;
this.upload_cancelled++;
if (triggerErrorEvent) {
this.Debug("Event: uploadError: File ID: " + this.current_file_item.id + ". Cancelled current upload");
ExternalCall.UploadError(this.uploadError_Callback, this.ERROR_CODE_FILE_CANCELLED, this.current_file_item.ToJavaScriptObject(), "File Upload Cancelled.");
} else {
this.Debug("Event: cancelUpload: File ID: " + this.current_file_item.id + ". Cancelled current upload. Suppressed uploadError event.");
}
this.UploadComplete(false);
} else if (file_id) {
// Find the file in the queue
var file_index:Number = this.FindIndexInFileQueue(file_id);
if (file_index >= 0) {
// Remove the file from the queue
file_item = FileItem(this.file_queue[file_index]);
file_item.file_status = FileItem.FILE_STATUS_CANCELLED;
this.file_queue[file_index] = null;
this.queued_uploads--;
this.upload_cancelled++;
// Cancel the file (just for good measure) and make the callback
file_item.file_reference.cancel();
this.removeFileReferenceEventListeners(file_item);
file_item.file_reference = null;
if (triggerErrorEvent) {
this.Debug("Event: uploadError : " + file_item.id + ". Cancelled queued upload");
ExternalCall.UploadError(this.uploadError_Callback, this.ERROR_CODE_FILE_CANCELLED, file_item.ToJavaScriptObject(), "File Cancelled");
} else {
this.Debug("Event: cancelUpload: File ID: " + file_item.id + ". Cancelled current upload. Suppressed uploadError event.");
}
// Get rid of the file object
file_item = null;
}
} else {
// Get the first file and cancel it
while (this.file_queue.length > 0 && file_item == null) {
// Check that File Reference is valid (if not make sure it's deleted and get the next one on the next loop)
file_item = FileItem(this.file_queue.shift()); // Cast back to a FileItem
if (typeof(file_item) == "undefined") {
file_item = null;
continue;
}
}
if (file_item != null) {
file_item.file_status = FileItem.FILE_STATUS_CANCELLED;
this.queued_uploads--;
this.upload_cancelled++;
// Cancel the file (just for good measure) and make the callback
file_item.file_reference.cancel();
this.removeFileReferenceEventListeners(file_item);
file_item.file_reference = null;
if (triggerErrorEvent) {
this.Debug("Event: uploadError : " + file_item.id + ". Cancelled queued upload");
ExternalCall.UploadError(this.uploadError_Callback, this.ERROR_CODE_FILE_CANCELLED, file_item.ToJavaScriptObject(), "File Cancelled");
} else {
this.Debug("Event: cancelUpload: File ID: " + file_item.id + ". Cancelled current upload. Suppressed uploadError event.");
}
// Get rid of the file object
file_item = null;
}
}
}
/* Requeues the indicated file. Returns true if successful or if the file is
* already in the queue. Otherwise returns false.
* */
private function RequeueUpload(fileIdentifier:*):Boolean {
var file:FileItem = null;
if (typeof(fileIdentifier) === "number") {
var fileIndex:Number = Number(fileIdentifier);
if (fileIndex >= 0 && fileIndex < this.file_index.length) {
file = this.file_index[fileIndex];
}
} else if (typeof(fileIdentifier) === "string") {
file = FindFileInFileIndex(String(fileIdentifier));
} else {
return false;
}
if (file !== null) {
if (file.file_status === FileItem.FILE_STATUS_IN_PROGRESS || file.file_status === FileItem.FILE_STATUS_NEW) {
return false;
} else if (file.file_status !== FileItem.FILE_STATUS_QUEUED) {
file.file_status = FileItem.FILE_STATUS_QUEUED;
this.file_queue.unshift(file);
this.queued_uploads++;
}
return true;
} else {
return false;
}
}
private function GetStats():Object {
return {
in_progress : this.current_file_item == null ? 0 : 1,
files_queued : this.queued_uploads,
successful_uploads : this.successful_uploads,
upload_errors : this.upload_errors,
upload_cancelled : this.upload_cancelled,
queue_errors : this.queue_errors
};
}
private function SetStats(stats:Object):void {
this.successful_uploads = typeof(stats["successful_uploads"]) === "number" ? stats["successful_uploads"] : this.successful_uploads;
this.upload_errors = typeof(stats["upload_errors"]) === "number" ? stats["upload_errors"] : this.upload_errors;
this.upload_cancelled = typeof(stats["upload_cancelled"]) === "number" ? stats["upload_cancelled"] : this.upload_cancelled;
this.queue_errors = typeof(stats["queue_errors"]) === "number" ? stats["queue_errors"] : this.queue_errors;
}
private function GetFile(file_id:String):Object {
var file_index:Number = this.FindIndexInFileQueue(file_id);
if (file_index >= 0) {
var file:FileItem = this.file_queue[file_index];
} else {
if (this.current_file_item != null) {
file = this.current_file_item;
} else {
for (var i:Number = 0; i < this.file_queue.length; i++) {
file = this.file_queue[i];
if (file != null) break;
}
}
}
if (file == null) {
return null;
} else {
return file.ToJavaScriptObject();
}
}
private function GetFileByIndex(index:Number):Object {
if (index < 0 || index > this.file_index.length - 1) {
return null;
} else {
return this.file_index[index].ToJavaScriptObject();
}
}
private function AddFileParam(file_id:String, name:String, value:String):Boolean {
var item:FileItem = this.FindFileInFileIndex(file_id);
if (item != null) {
item.AddParam(name, value);
return true;
}
else {
return false;
}
}
private function RemoveFileParam(file_id:String, name:String):Boolean {
var item:FileItem = this.FindFileInFileIndex(file_id);
if (item != null) {
item.RemoveParam(name);
return true;
}
else {
return false;
}
}
private function SetUploadURL(url:String):void {
if (typeof(url) !== "undefined" && url !== "") {
this.uploadURL = url;
}
}
private function SetPostParams(post_object:Object):void {
if (typeof(post_object) !== "undefined" && post_object !== null) {
this.uploadPostObject = post_object;
}
}
private function SetFileTypes(types:String, description:String):void {
this.fileTypes = types;
this.fileTypesDescription = description;
this.LoadFileExensions(this.fileTypes);
}
// Sets the file size limit. Accepts size values with units: 100 b, 1KB, 23Mb, 4 Gb
// Parsing is not robust. "100 200 MB KB B GB" parses as "100 MB"
private function SetFileSizeLimit(size:String):void {
var value:Number = 0;
var unit:String = "kb";
// Trim the string
var trimPattern:RegExp = /^\s*|\s*$/;
size = size.toLowerCase();
size = size.replace(trimPattern, "");
// Get the value part
var values:Array = size.match(/^\d+/);
if (values !== null && values.length > 0) {
value = parseInt(values[0]);
}
if (isNaN(value) || value < 0) value = 0;
// Get the units part
var units:Array = size.match(/(b|kb|mb|gb)/);
if (units != null && units.length > 0) {
unit = units[0];
}
// Set the multiplier for converting the unit to bytes
var multiplier:Number = 1024;
if (unit === "b")
multiplier = 1;
else if (unit === "mb")
multiplier = 1048576;
else if (unit === "gb")
multiplier = 1073741824;
this.fileSizeLimit = value * multiplier;
}
private function SetFileUploadLimit(file_upload_limit:Number):void {
if (file_upload_limit < 0) file_upload_limit = 0;
this.fileUploadLimit = file_upload_limit;
}
private function SetFileQueueLimit(file_queue_limit:Number):void {
if (file_queue_limit < 0) file_queue_limit = 0;
this.fileQueueLimit = file_queue_limit;
}
private function SetFilePostName(file_post_name:String):void {
if (file_post_name != "") {
this.filePostName = file_post_name;
}
}
private function SetUseQueryString(use_query_string:Boolean):void {
this.useQueryString = use_query_string;
}
private function SetRequeueOnError(requeue_on_error:Boolean):void {
this.requeueOnError = requeue_on_error;
}
private function SetHTTPSuccess(http_status_codes:*):void {
this.httpSuccess = [];
if (typeof http_status_codes === "string") {
var status_code_strings:Array = http_status_codes.replace(" ", "").split(",");
for each (var http_status_string:String in status_code_strings)
{
try {
this.httpSuccess.push(Number(http_status_string));
} catch (ex:Object) {
// Ignore errors
this.Debug("Could not add HTTP Success code: " + http_status_string);
}
}
}
else if (typeof http_status_codes === "object" && typeof http_status_codes.length === "number") {
for each (var http_status:* in http_status_codes)
{
try {
this.Debug("adding: " + http_status);
this.httpSuccess.push(Number(http_status));
} catch (ex:Object) {
this.Debug("Could not add HTTP Success code: " + http_status);
}
}
}
}
private function SetAssumeSuccessTimeout(timeout_seconds:Number):void {
this.assumeSuccessTimeout = timeout_seconds < 0 ? 0 : timeout_seconds;
}
private function SetDebugEnabled(debug_enabled:Boolean):void {
this.debugEnabled = debug_enabled;
}
/* *************************************************************
Button Handling Functions
*************************************************************** */
private function SetButtonImageURL(button_image_url:String):void {
this.buttonImageURL = button_image_url;
try {
if (this.buttonImageURL !== null && this.buttonImageURL !== "") {
this.buttonLoader.load(new URLRequest(this.buttonImageURL));
}
} catch (ex:Object) {
}
}
private function ButtonClickHandler(e:MouseEvent):void {
if (!this.buttonStateDisabled) {
if (this.buttonAction === this.BUTTON_ACTION_SELECT_FILE) {
this.SelectFile();
}
else if (this.buttonAction === this.BUTTON_ACTION_START_UPLOAD) {
this.StartUpload();
}
else {
this.SelectFiles();
}
}
}
private function UpdateButtonState():void {
var xOffset:Number = 0;
var yOffset:Number = 0;
this.buttonLoader.x = xOffset;
this.buttonLoader.y = yOffset;
if (this.buttonStateDisabled) {
this.buttonLoader.y = this.buttonHeight * -3 + yOffset;
}
else if (this.buttonStateMouseDown) {
this.buttonLoader.y = this.buttonHeight * -2 + yOffset;
}
else if (this.buttonStateOver) {
this.buttonLoader.y = this.buttonHeight * -1 + yOffset;
}
else {
this.buttonLoader.y = -yOffset;
}
};
private function SetButtonDimensions(width:Number = -1, height:Number = -1):void {
if (width >= 0) {
this.buttonWidth = width;
}
if (height >= 0) {
this.buttonHeight = height;
}
this.buttonTextField.width = this.buttonWidth;
this.buttonTextField.height = this.buttonHeight;
this.buttonCursorSprite.width = this.buttonWidth;
this.buttonCursorSprite.height = this.buttonHeight;
this.UpdateButtonState();
}
private function SetButtonText(button_text:String):void {
this.buttonText = button_text;
this.SetButtonTextStyle(this.buttonTextStyle);
}
private function SetButtonTextStyle(button_text_style:String):void {
this.buttonTextStyle = button_text_style;
var style:StyleSheet = new StyleSheet();
style.parseCSS(this.buttonTextStyle);
this.buttonTextField.styleSheet = style;
this.buttonTextField.htmlText = '<span class="button-text">' + this.buttonText + '</span>';
}
private function SetButtonTextPadding(left:Number, top:Number):void {
this.buttonTextField.x = this.buttonTextLeftPadding = left;
this.buttonTextField.y = this.buttonTextTopPadding = top;
}
private function SetButtonDisabled(disabled:Boolean):void {
this.buttonStateDisabled = disabled;
this.UpdateButtonState();
}
private function SetButtonAction(button_action:Number):void {
this.buttonAction = button_action;
}
private function SetButtonCursor(button_cursor:Number):void {
this.buttonCursor = button_cursor;
this.buttonCursorSprite.useHandCursor = (button_cursor === this.BUTTON_CURSOR_HAND);
}
/* *************************************************************
File processing and handling functions
*************************************************************** */
private function StartUpload(file_id:String = ""):void {
// Only upload a file uploads are being processed.
if (this.current_file_item != null) {
this.Debug("StartUpload(): Upload already in progress. Not starting another upload.");
return;
}
this.Debug("StartUpload: " + (file_id ? "File ID: " + file_id : "First file in queue"));
// Check the upload limit
if (this.successful_uploads >= this.fileUploadLimit && this.fileUploadLimit != 0) {
this.Debug("Event: uploadError : Upload limit reached. No more files can be uploaded.");
ExternalCall.UploadError(this.uploadError_Callback, this.ERROR_CODE_UPLOAD_LIMIT_EXCEEDED, null, "The upload limit has been reached.");
this.current_file_item = null;
return;
}
// Get the next file to upload
if (!file_id) {
while (this.file_queue.length > 0 && this.current_file_item == null) {
this.current_file_item = FileItem(this.file_queue.shift());
if (typeof(this.current_file_item) == "undefined") {
this.current_file_item = null;
}
}
} else {
var file_index:Number = this.FindIndexInFileQueue(file_id);
if (file_index >= 0) {
// Set the file as the current upload and remove it from the queue
this.current_file_item = FileItem(this.file_queue[file_index]);
this.file_queue[file_index] = null;
} else {
this.Debug("Event: uploadError : File ID not found in queue: " + file_id);
ExternalCall.UploadError(this.uploadError_Callback, this.ERROR_CODE_SPECIFIED_FILE_ID_NOT_FOUND, null, "File ID not found in the queue.");
}
}
if (this.current_file_item != null) {
// Trigger the uploadStart event which will call ReturnUploadStart to begin the actual upload
this.Debug("Event: uploadStart : File ID: " + this.current_file_item.id);
this.current_file_item.file_status = FileItem.FILE_STATUS_IN_PROGRESS;
ExternalCall.UploadStart(this.uploadStart_Callback, this.current_file_item.ToJavaScriptObject());
}
// Otherwise we've would have looped through all the FileItems. This means the queue is empty)
else {
this.Debug("StartUpload(): No files found in the queue.");
}
}
// This starts the upload when the user returns TRUE from the uploadStart event. Rather than just have the value returned from
// the function we do a return function call so we can use the setTimeout work-around for Flash/JS circular calls.
private function ReturnUploadStart(start_upload:Boolean):void {
if (this.current_file_item == null) {
this.Debug("ReturnUploadStart called but no file was prepped for uploading. The file may have been cancelled or stopped.");
return;
}
var js_object:Object;
if (start_upload) {
try {
// Set the event handlers
this.current_file_item.file_reference.addEventListener(Event.OPEN, this.Open_Handler);
this.current_file_item.file_reference.addEventListener(ProgressEvent.PROGRESS, this.FileProgress_Handler);
this.current_file_item.file_reference.addEventListener(IOErrorEvent.IO_ERROR, this.IOError_Handler);
this.current_file_item.file_reference.addEventListener(SecurityErrorEvent.SECURITY_ERROR, this.SecurityError_Handler);
this.current_file_item.file_reference.addEventListener(HTTPStatusEvent.HTTP_STATUS, this.HTTPError_Handler);
this.current_file_item.file_reference.addEventListener(Event.COMPLETE, this.Complete_Handler);
this.current_file_item.file_reference.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, this.ServerData_Handler);
// Get the request (post values, etc)
var request:URLRequest = this.BuildRequest();
if (this.uploadURL.length == 0) {
this.Debug("Event: uploadError : IO Error : File ID: " + this.current_file_item.id + ". Upload URL string is empty.");
// Remove the event handlers
this.removeFileReferenceEventListeners(this.current_file_item);
this.current_file_item.file_status = FileItem.FILE_STATUS_QUEUED;
this.file_queue.unshift(this.current_file_item);
js_object = this.current_file_item.ToJavaScriptObject();
this.current_file_item = null;
ExternalCall.UploadError(this.uploadError_Callback, this.ERROR_CODE_MISSING_UPLOAD_URL, js_object, "Upload URL string is empty.");
} else {
this.Debug("ReturnUploadStart(): File accepted by startUpload event and readied for upload. Starting upload to " + request.url + " for File ID: " + this.current_file_item.id);
this.current_file_item.file_status = FileItem.FILE_STATUS_IN_PROGRESS;
this.current_file_item.file_reference.upload(request, this.filePostName, false);
}
} catch (ex:Error) {
this.Debug("ReturnUploadStart: Exception occurred: " + message);
this.upload_errors++;
this.current_file_item.file_status = FileItem.FILE_STATUS_ERROR;
var message:String = ex.errorID + "\n" + ex.name + "\n" + ex.message + "\n" + ex.getStackTrace();
this.Debug("Event: uploadError(): Upload Failed. Exception occurred: " + message);
ExternalCall.UploadError(this.uploadError_Callback, this.ERROR_CODE_UPLOAD_FAILED, this.current_file_item.ToJavaScriptObject(), message);
this.UploadComplete(true);
}
} else {
// Remove the event handlers
this.removeFileReferenceEventListeners(this.current_file_item);
// Re-queue the FileItem
this.current_file_item.file_status = FileItem.FILE_STATUS_QUEUED;
js_object = this.current_file_item.ToJavaScriptObject();
this.file_queue.unshift(this.current_file_item);
this.current_file_item = null;
this.Debug("Event: uploadError : Call to uploadStart returned false. Not uploading the file.");
ExternalCall.UploadError(this.uploadError_Callback, this.ERROR_CODE_FILE_VALIDATION_FAILED, js_object, "Call to uploadStart return false. Not uploading file.");
this.Debug("Event: uploadComplete : Call to uploadStart returned false. Not uploading the file.");
ExternalCall.UploadComplete(this.uploadComplete_Callback, js_object);
}
}
// Completes the file upload by deleting it's reference, advancing the pointer.
// Once this event fires a new upload can be started.
private function UploadComplete(eligible_for_requeue:Boolean):void {
var jsFileObj:Object = this.current_file_item.ToJavaScriptObject();
this.removeFileReferenceEventListeners(this.current_file_item);
if (!eligible_for_requeue || this.requeueOnError == false) {
this.current_file_item.file_reference = null;
this.queued_uploads--;
} else if (this.requeueOnError == true) {
this.current_file_item.file_status = FileItem.FILE_STATUS_QUEUED;
this.file_queue.unshift(this.current_file_item);
}
this.current_file_item = null;
this.Debug("Event: uploadComplete : Upload cycle complete.");
ExternalCall.UploadComplete(this.uploadComplete_Callback, jsFileObj);
}
/* *************************************************************
Utility Functions
*************************************************************** */
// Check the size of the file against the allowed file size. If it is less the return TRUE. If it is too large return FALSE
private function CheckFileSize(file_item:FileItem):Number {
if (file_item.file_reference.size == 0) {
return this.SIZE_ZERO_BYTE;
} else if (this.fileSizeLimit != 0 && file_item.file_reference.size > this.fileSizeLimit) {
return this.SIZE_TOO_BIG;
} else {
return this.SIZE_OK;
}
}
private function CheckFileType(file_item:FileItem):Boolean {
// If no extensions are defined then a *.* was passed and the check is unnecessary
if (this.valid_file_extensions.length == 0) {
return true;
}
var fileRef:FileReference = file_item.file_reference;
var last_dot_index:Number = fileRef.name.lastIndexOf(".");
var extension:String = "";
if (last_dot_index >= 0) {
extension = fileRef.name.substr(last_dot_index + 1).toLowerCase();
}
var is_valid_filetype:Boolean = false;
for (var i:Number=0; i < this.valid_file_extensions.length; i++) {
if (String(this.valid_file_extensions[i]) == extension) {
is_valid_filetype = true;
break;
}
}
return is_valid_filetype;
}
private function BuildRequest():URLRequest {
// Create the request object
var request:URLRequest = new URLRequest();
request.method = URLRequestMethod.POST;
var file_post:Object = this.current_file_item.GetPostObject();
if (this.useQueryString) {
var pairs:Array = new Array();
for (key in this.uploadPostObject) {
this.Debug("Global URL Item: " + key + "=" + this.uploadPostObject[key]);
if (this.uploadPostObject.hasOwnProperty(key)) {
pairs.push(escape(key) + "=" + escape(this.uploadPostObject[key]));
}
}
for (key in file_post) {
this.Debug("File Post Item: " + key + "=" + file_post[key]);
if (file_post.hasOwnProperty(key)) {
pairs.push(escape(key) + "=" + escape(file_post[key]));
}
}
request.url = this.uploadURL + (this.uploadURL.indexOf("?") > -1 ? "&" : "?") + pairs.join("&");
} else {
var key:String;
var post:URLVariables = new URLVariables();
for (key in this.uploadPostObject) {
this.Debug("Global Post Item: " + key + "=" + this.uploadPostObject[key]);
if (this.uploadPostObject.hasOwnProperty(key)) {
post[key] = this.uploadPostObject[key];
}
}
for (key in file_post) {
this.Debug("File Post Item: " + key + "=" + file_post[key]);
if (file_post.hasOwnProperty(key)) {
post[key] = file_post[key];
}
}
request.url = this.uploadURL;
request.data = post;
}
return request;
}
private function Debug(msg:String):void {
try {
if (this.debugEnabled) {
var lines:Array = msg.split("\n");
for (var i:Number=0; i < lines.length; i++) {
lines[i] = "SWF DEBUG: " + lines[i];
}
ExternalCall.Debug(this.debug_Callback, lines.join("\n"));
}
} catch (ex:Error) {
// pretend nothing happened
trace(ex);
}
}
private function PrintDebugInfo():void {
var debug_info:String = "\n----- SWF DEBUG OUTPUT ----\n";
debug_info += "Build Number: " + this.build_number + "\n";
debug_info += "movieName: " + this.movieName + "\n";
debug_info += "Upload URL: " + this.uploadURL + "\n";
debug_info += "File Types String: " + this.fileTypes + "\n";
debug_info += "Parsed File Types: " + this.valid_file_extensions.toString() + "\n";
debug_info += "HTTP Success: " + this.httpSuccess.join(", ") + "\n";
debug_info += "File Types Description: " + this.fileTypesDescription + "\n";
debug_info += "File Size Limit: " + this.fileSizeLimit + " bytes\n";
debug_info += "File Upload Limit: " + this.fileUploadLimit + "\n";
debug_info += "File Queue Limit: " + this.fileQueueLimit + "\n";
debug_info += "Post Params:\n";
for (var key:String in this.uploadPostObject) {
if (this.uploadPostObject.hasOwnProperty(key)) {
debug_info += " " + key + "=" + this.uploadPostObject[key] + "\n";
}
}
debug_info += "----- END SWF DEBUG OUTPUT ----\n";
this.Debug(debug_info);
}
private function FindIndexInFileQueue(file_id:String):Number {
for (var i:Number = 0; i < this.file_queue.length; i++) {
var item:FileItem = this.file_queue[i];
if (item != null && item.id == file_id) return i;
}
return -1;
}
private function FindFileInFileIndex(file_id:String):FileItem {
for (var i:Number = 0; i < this.file_index.length; i++) {
var item:FileItem = this.file_index[i];
if (item != null && item.id == file_id) return item;
}
return null;
}
// Parse the file extensions in to an array so we can validate them agains
// the files selected later.
private function LoadFileExensions(filetypes:String):void {
var extensions:Array = filetypes.split(";");
this.valid_file_extensions = new Array();
for (var i:Number=0; i < extensions.length; i++) {
var extension:String = String(extensions[i]);
var dot_index:Number = extension.lastIndexOf(".");
if (dot_index >= 0) {
extension = extension.substr(dot_index + 1).toLowerCase();
} else {
extension = extension.toLowerCase();
}
// If one of the extensions is * then we allow all files
if (extension == "*") {
this.valid_file_extensions = new Array();
break;
}
this.valid_file_extensions.push(extension);
}
}
private function loadPostParams(param_string:String):void {
var post_object:Object = {};
if (param_string != null) {
var name_value_pairs:Array = param_string.split("&");
for (var i:Number = 0; i < name_value_pairs.length; i++) {
var name_value:String = String(name_value_pairs[i]);
var index_of_equals:Number = name_value.indexOf("=");
if (index_of_equals > 0) {
post_object[decodeURIComponent(name_value.substring(0, index_of_equals))] = decodeURIComponent(name_value.substr(index_of_equals + 1));
}
}
}
this.uploadPostObject = post_object;
}
private function removeFileReferenceEventListeners(file_item:FileItem):void {
if (file_item != null && file_item.file_reference != null) {
file_item.file_reference.removeEventListener(Event.OPEN, this.Open_Handler);
file_item.file_reference.removeEventListener(ProgressEvent.PROGRESS, this.FileProgress_Handler);
file_item.file_reference.removeEventListener(IOErrorEvent.IO_ERROR, this.IOError_Handler);
file_item.file_reference.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, this.SecurityError_Handler);
file_item.file_reference.removeEventListener(HTTPStatusEvent.HTTP_STATUS, this.HTTPError_Handler);
file_item.file_reference.removeEventListener(DataEvent.UPLOAD_COMPLETE_DATA, this.ServerData_Handler);
}
}
public function htmlEscape(str:String):String {
return XML( new XMLNode( XMLNodeType.TEXT_NODE, str ) ).toXMLString();
}
}
}
|