📄 swfupload.as
字号:
// 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;
}
}
// 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;
// The the file to see if it is acceptable
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) {
this.Debug("Event: fileQueued : File ID: " + file_item.id);
this.file_queue.push(file_item);
this.queued_uploads++;
num_files_queued++;
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.Debug("Entered an unexpected state checking the file in Select_Handler");
}
}
}
this.Debug("Event: fileDialogComplete : Finished adding files");
ExternalCall.FileDialogComplete(this.fileDialogComplete_Callback, file_reference_list.length, num_files_queued);
}
/* ****************************************************************
Externally exposed functions
****************************************************************** */
// Opens a file browser dialog that allows one file to be selected.
private function SelectFile():Void {
this.fileBrowserOne = new FileReference();
var fbo_listener:Object = {
onSelect : Delegate.create(this, this.Select_One_Handler),
onCancel : Delegate.create(this, this.DialogCancelled_Handler)
}
this.fileBrowserOne.addListener(fbo_listener);
// 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);
this.fileBrowserOne.browse([{ description : allowed_file_types_description, extension : allowed_file_types }]);
}
// 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);
this.fileBrowserMany.browse([{ description : allowed_file_types_description, extension : allowed_file_types }]);
}
// 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();
this.current_file_item.file_status = FileItem.FILE_STATUS_QUEUED;
// Remove the event handlers
this.current_file_item.file_reference.removeListener(this.file_reference_listener);
this.file_queue.unshift(this.current_file_item);
var js_object:Object = this.current_file_item.ToJavaScriptObject();
this.current_file_item = null;
ExternalCall.UploadError(this.uploadError_Callback, this.ERROR_CODE_UPLOAD_STOPPED, js_object, "Upload Stopped");
ExternalCall.UploadComplete(this.uploadComplete_Callback, js_object);
this.Debug("StopUpload(): upload stopped.");
} else {
this.Debug("StopUpload(): Upload run not in progress");
}
}
/* 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):Void {
var file_item:FileItem = null;
if (file_id == undefined) file_id = "";
// 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++;
this.Debug("Event: fileCancelled: File ID: " + this.current_file_item.id + ". Cancelling current upload");
ExternalCall.UploadError(this.uploadError_Callback, this.ERROR_CODE_FILE_CANCELLED, this.current_file_item.ToJavaScriptObject(), "File Upload Cancelled.");
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();
file_item.file_reference.removeListener(this.file_reference_listener);
file_item.file_reference = null;
this.Debug("Event: uploadError : " + file_item.id + ". Cancelling queued upload");
this.Debug("Event: uploadError : " + file_item.id + ". Cancelling queued upload");
ExternalCall.UploadError(this.uploadError_Callback, this.ERROR_CODE_FILE_CANCELLED, file_item.ToJavaScriptObject(), "File Cancelled");
// 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();
file_item.file_reference.removeListener(this.file_reference_listener);
file_item.file_reference = null;
this.Debug("Event: uploadError : " + file_item.id + ". Cancelling queued upload");
ExternalCall.UploadError(this.uploadError_Callback, this.ERROR_CODE_FILE_CANCELLED, file_item.ToJavaScriptObject(), "File Cancelled");
// Get rid of the file object
file_item = null;
}
}
}
private function SetCredentials(name:String, password:String):Void {
this.Debug("Credentials are not supported in SWFUpload for Flash 8");
}
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:FileItem = null;
var file_index:Number = this.FindIndexInFileQueue(file_id);
if (file_index >= 0) {
file = 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);
}
// Set the file size limit. Accepts a number and a unit. If the unit is
// omitted then kilobytes is assumed.
// This method does not provide robust parsing but should be sufficient
// ie. "1000 200 KB MB GB" is parsed as 1000 KB because it begins with 1000 and has "KB" first
// If a value cannot be parsed then the limit is set to zero which SWFUpload treats as unlimited
private function SetFileSizeLimit(size:String):Void {
var value:Number = 0;
var unit:String = "kb";
// Trim white space
size = size.toLowerCase();
//size = trim(size);
// Get Value part
value = parseInt(size, 10);
if (isNaN(value) || value < 0) value = 0;
// Get Unit part
var b_idx = size.indexOf("b");
if (b_idx > 0) {
unit = size.substr(b_idx - 1, b_idx);
} else if (b_idx === 0) {
unit = "b";
}
// Determine Multiplier
var multiplier:Number = 1024;
if (unit === "b")
multiplier = 1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -