📄 swfupload.as
字号:
/*
* Todo:
* In SWFUpload v3 the file_queue and file_index should be merged. This probably means we'll remove FileItem.id and just
* use indexes everywhere.
* */
import flash.net.FileReferenceList;
import flash.net.FileReference;
import flash.external.ExternalInterface;
import FileItem;
import ExternalCall;
import Delegate;
class SWFUpload {
// Cause SWFUpload to start as soon as the movie starts
public static function main():Void
{
// Code to attempt to fix "flash script running slowly" error messages
var counter:Number = 0;
_root.onEnterFrame = function () {
if (++counter > 100) counter = 0;
};
// Start SWFUpload
var SWFUpload:SWFUpload = new SWFUpload();
}
private var build_number:String = "SWFUPLOAD 2.1.0 FP8 2008-03-13";
// 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 file_reference_listener:Object;
// 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;
// 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 requeueOnError:Boolean = false;
private var debugEnabled: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;
public function SWFUpload() {
// Do the feature detection to determine if this Flash version supports the functionality we need.
if (!flash.net.FileReferenceList || !flash.net.FileReference || !flash.external.ExternalInterface || !flash.external.ExternalInterface.available) {
return;
}
System.security.allowDomain("*"); // Allow uploading to any domain
// Setup file FileReferenceList events
var fbmListener:Object = {
onSelect : Delegate.create(this, this.Select_Many_Handler),
onCancel : Delegate.create(this, this.DialogCancelled_Handler)
}
this.fileBrowserMany.addListener(fbmListener);
// Setup the events listner
this.file_reference_listener = {
onOpen : Delegate.create(this, this.Open_Handler),
onProgress : Delegate.create(this, this.FileProgress_Handler),
onIOError : Delegate.create(this, this.IOError_Handler),
onSecurityError : Delegate.create(this, this.SecurityError_Handler),
onHTTPError : Delegate.create(this, this.HTTPError_Handler),
onComplete : Delegate.create(this, this.FileComplete_Handler)
};
// Get the move name
this.movieName = _root.movieName;
// **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";
// Get the Flash Vars
this.uploadURL = _root.uploadURL;
this.filePostName = _root.filePostName;
this.fileTypes = _root.fileTypes;
this.fileTypesDescription = _root.fileTypesDescription + " (" + this.fileTypes + ")";
this.loadPostParams(_root.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.debugEnabled == "true" ? true : false;
} catch (ex:Object) {
this.debugEnabled = false;
}
try {
this.SetFileSizeLimit(_root.fileSizeLimit);
} catch (ex:Object) {
this.fileSizeLimit = 0;
}
try {
this.fileUploadLimit = Number(_root.fileUploadLimit);
if (this.fileUploadLimit < 0) this.fileUploadLimit = 0;
} catch (ex:Object) {
this.fileUploadLimit = 0;
}
try {
this.fileQueueLimit = Number(_root.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.requeueOnError = _root.requeueOnError == "true" ? true : false;
} catch (ex:Object) {
this.requeueOnError = false;
}
try {
ExternalInterface.addCallback("SelectFile", this, this.SelectFile);
ExternalInterface.addCallback("SelectFiles", this, this.SelectFiles);
ExternalInterface.addCallback("StartUpload", this, this.StartUpload);
ExternalInterface.addCallback("ReturnUploadStart", this, this.ReturnUploadStart);
ExternalInterface.addCallback("StopUpload", this, this.StopUpload);
ExternalInterface.addCallback("CancelUpload", this, this.CancelUpload);
ExternalInterface.addCallback("GetStats", this, this.GetStats);
ExternalInterface.addCallback("SetStats", this, this.SetStats);
ExternalInterface.addCallback("GetFile", this, this.GetFile);
ExternalInterface.addCallback("GetFileByIndex", this, this.GetFileByIndex);
ExternalInterface.addCallback("AddFileParam", this, this.AddFileParam);
ExternalInterface.addCallback("RemoveFileParam", this, this.RemoveFileParam);
ExternalInterface.addCallback("SetUploadURL", this, this.SetUploadURL);
ExternalInterface.addCallback("SetPostParams", this, this.SetPostParams);
ExternalInterface.addCallback("SetFileTypes", this, this.SetFileTypes);
ExternalInterface.addCallback("SetFileSizeLimit", this, this.SetFileSizeLimit);
ExternalInterface.addCallback("SetFileUploadLimit", this, this.SetFileUploadLimit);
ExternalInterface.addCallback("SetFileQueueLimit", this, this.SetFileQueueLimit);
ExternalInterface.addCallback("SetFilePostName", this, this.SetFilePostName);
ExternalInterface.addCallback("SetUseQueryString", this, this.SetUseQueryString);
ExternalInterface.addCallback("SetRequeueOnError", this, this.SetRequeueOnError);
ExternalInterface.addCallback("SetDebugEnabled", this, this.SetDebugEnabled);
} catch (ex:Error) {
this.Debug("Callbacks where not set.");
return;
}
this.Debug("SWFUpload Init Complete");
this.PrintDebugInfo();
ExternalCall.Simple(this.flashReady_Callback);
}
/* *****************************************
* FileReference Event Handlers
* *************************************** */
private function DialogCancelled_Handler():Void {
this.Debug("Event: fileDialogComplete: File Dialog window cancelled.");
ExternalCall.FileDialogComplete(this.fileDialogComplete_Callback, 0, 0);
}
private function Open_Handler(file:FileReference):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(file:FileReference, bytesLoaded:Number, bytesTotal:Number):Void {
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 FileComplete_Handler():Void {
this.successful_uploads++;
this.current_file_item.file_status = FileItem.FILE_STATUS_SUCCESS;
this.Debug("Event: uploadSuccess: File ID: " + this.current_file_item.id + " Data: n/a in Flash 8");
ExternalCall.UploadSuccess(this.uploadSuccess_Callback, this.current_file_item.ToJavaScriptObject());
this.UploadComplete(false);
}
private function HTTPError_Handler(file:FileReference, httpError:Number):Void {
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: " + httpError + ".");
ExternalCall.UploadError(this.uploadError_Callback, this.ERROR_CODE_HTTP_ERROR, this.current_file_item.ToJavaScriptObject(), httpError.toString());
this.UploadComplete(true);
}
// 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 the crash the browser (FireFox/Opera)
private function IOError_Handler(file:FileReference):Void {
this.upload_errors++;
this.current_file_item.file_status = FileItem.FILE_STATUS_ERROR;
if(this.uploadURL.length == 0) {
this.Debug("Event: uploadError : IO Error : File ID: " + this.current_file_item.id + ". Upload URL string is empty.");
ExternalCall.UploadError(this.uploadError_Callback, this.ERROR_CODE_MISSING_UPLOAD_URL, this.current_file_item.ToJavaScriptObject(), "IO Error");
} else {
this.Debug("Event: uploadError : IO Error : File ID: " + this.current_file_item.id + ". IO Error.");
ExternalCall.UploadError(this.uploadError_Callback, this.ERROR_CODE_IO_ERROR, this.current_file_item.ToJavaScriptObject(), "IO Error");
}
this.UploadComplete(true);
}
private function SecurityError_Handler(file:FileReference, errorString:String):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:" + errorString);
ExternalCall.UploadError(this.uploadError_Callback, this.ERROR_CODE_SECURITY_ERROR, this.current_file_item.ToJavaScriptObject(), errorString);
this.UploadComplete(true);
}
private function Select_Many_Handler(frl:FileReferenceList):Void {
this.Select_Handler(frl.fileList);
}
private function Select_One_Handler(file:FileReference):Void {
var fileArray:Array = new Array(1);
fileArray[0] = file;
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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -