⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 swfupload.as

📁 用flash做的网页上传工具
💻 AS
📖 第 1 页 / 共 3 页
字号:
		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 {
		// Nothing to do. Here only for API compatibility
	}

	private function SetRequeueOnError(requeue_on_error:Boolean):Void {
		this.requeueOnError = requeue_on_error;
	}

	private function SetDebugEnabled(debug_enabled:Boolean):Void {
		this.debugEnabled = debug_enabled;
	}
	
	/* *************************************************************
		File processing and handling functions
	*************************************************************** */
	private function StartUpload(file_id:String):Void {
		if (file_id == undefined) file_id = "";
		
		// Only upload a file uploads are being processed.
		//   startFile could be called by a file cancellation even when we aren't currently uploading
		if (this.current_file_item != null) {
			this.Debug("StartUpload(): Upload already in progress. Not starting another upload.");
		}

		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.");
			}
		}


		// Start the upload if we found an item to upload
		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);
			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;
		}
		
		if (start_upload) {
			try {
				// Set the event handlers
				this.current_file_item.file_reference.addListener(this.file_reference_listener);
				
				// Build the request (post params, etc)
				var url:String = this.BuildRequest();
				
				this.Debug("ReturnUploadStart(): File accepted by startUpload event and readied for upload.  Starting upload to " + url + " for File ID: " + this.current_file_item.id);
				this.current_file_item.file_reference.upload(url, this.filePostName, false);
			} catch (ex:Error) {
				this.Debug("ReturnUploadStart: Exception occurred: " + ex);

				this.upload_errors++;
				this.current_file_item.file_status = FileItem.FILE_STATUS_ERROR;
				var message:String = ex.name + "\n" + ex.message;

				this.Debug("Event: uploadError(): Unhandled exception: " + message);
				ExternalCall.UploadError(this.uploadError_Callback, this.ERROR_CODE_UPLOAD_FAILED, this.current_file_item.ToJavaScriptObject(), message);
				
				this.UploadComplete(false);
			}
			this.current_file_item.file_status = FileItem.FILE_STATUS_IN_PROGRESS;

		} else {
			
			// Remove the event handlers
			this.current_file_item.file_reference.removeListener(this.file_reference_listener);

			// Re-queue the FileItem
			this.current_file_item.file_status = FileItem.FILE_STATUS_QUEUED;
			var js_object: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 the 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 files a new upload can be started.
	private function UploadComplete(eligibile_for_requeue:Boolean):Void {
		var jsFileObj:Object = this.current_file_item.ToJavaScriptObject();
		
		this.current_file_item.file_reference.removeListener(this.file_reference_listener);
		
		if (!eligibile_for_requeue || this.requeueOnError == false) {
			this.current_file_item.file_reference = null;
			this.queued_uploads--;
		} else {
			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.
	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():String {
		// Create the request object
		var file_post:Object = this.current_file_item.GetPostObject();
		var key:String;

		var url:String = this.uploadURL;
		
		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(key + "=" + this.uploadPostObject[key]);
			}
		}

		for (key in file_post) {
			this.Debug("File Post Item: " + key + "=" + this.uploadPostObject[key]);
			if (file_post.hasOwnProperty(key)) {
				pairs.push(escape(key) + "=" + escape(file_post[key]));
			}
		}
		
		url = this.uploadURL  + (this.uploadURL.indexOf("?") > -1 ? "&" : "?") + pairs.join("&");
			
		return url;
	}
	
	private function Debug(msg:String):Void {
		if (this.debugEnabled) {
			var lines:Array = msg.split("\n");
			for (var i:Number=0; i < lines.length; i++) {
				lines[i] = "SWF DEBUG: " + lines[i];
			}
			try {
				ExternalCall.Debug(this.debug_Callback, lines.join("\n"));
			} catch (ex:Error) {
				// pretend nothing happened
			}
		}
	}

	private function PrintDebugInfo():Void {
		var debug_info:String = "\n----- SWF DEBUG OUTPUT ----\n"
			+ "Build Number:           " + this.build_number + "\n"
			+ "movieName:              " + this.movieName + "\n"
			+ "Upload URL:             " + this.uploadURL + "\n"
			+ "File Types String:      " + this.fileTypes + "\n"
			+ "Parsed File Types:      " + this.valid_file_extensions.toString() + "\n"
			+ "File Types Description: " + this.fileTypesDescription + "\n"
			+ "File Size Limit:        " + this.fileSizeLimit + " bytes\n"
			+ "File Upload Limit:      " + this.fileUploadLimit + "\n"
			+ "File Queue Limit:       " + this.fileQueueLimit + "\n"
			+ "Post Params:\n";
		for (var key:String in this.uploadPostObject) {
			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("&amp;");
			
			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[unescape(name_value.substring(0, index_of_equals))] = unescape(name_value.substr(index_of_equals + 1));
				}
			}
		}
		this.uploadPostObject = post_object;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -