📄 uploader.as
字号:
super.dispatchEventToJavaScript(newEvent); this.currentUploadThreads--; // get next off of queue: if(filesToUpload.length > 0) { processQueue(); } } private function uploadCompleteData (event:DataEvent) : void { logMessage("Got data back for " + fileIDList[event.target] + ": "); logMessage(event.data); var newEvent:Object = new Object(); newEvent.id = fileIDList[event.target]; newEvent.data = event.data; newEvent.type = "uploadCompleteData" super.dispatchEventToJavaScript(newEvent); } private function uploadCancel (event:Event) : void { logMessage("Canceled upload for " + fileIDList[event.target]); var newEvent:Object = new Object(); newEvent.id = fileIDList[event.target]; newEvent.type = "uploadCancel"; super.dispatchEventToJavaScript(newEvent); } private function uploadError (event:Event) : void { var newEvent:Object = {}; if (event is HTTPStatusEvent) { var myev:HTTPStatusEvent = event as HTTPStatusEvent; newEvent.status = myev.status; logMessage("HTTP status error for " + fileIDList[event.target] + ": " + myev.status); } else if (event is IOErrorEvent) { newEvent.status = event.toString(); logMessage("IO error for " + fileIDList[event.target] + ". Likely causes are problems with Internet connection or server misconfiguration."); } else if (event is SecurityErrorEvent) { newEvent.status = event.toString(); logMessage("Security error for " + fileIDList[event.target]); } newEvent.type = "uploadError"; newEvent.id = fileIDList[event.target]; super.dispatchEventToJavaScript(newEvent); // get next off of queue: if(filesToUpload.length > 0) { processQueue(); } } // Fired when the user selects a single file private function singleFileSelected(event:Event):void { this.clearFileList(); addFile(event.target as FileReference); processSelection(); } // Fired when the user selects multiple files private function multipleFilesSelected(event:Event):void { var currentFRL:FileReferenceList = multipleFiles; for each (var currentFR:FileReference in currentFRL.fileList) { addFile(currentFR); } processSelection(); } private function renderAsButton (buttonSkinSprite:String) : void { buttonSkin.load(new URLRequest(buttonSkinSprite)); var _this:Uploader = this; var initLoader:Function = function (event:Event) : void { buttonSprite.addChild(buttonSkin); buttonHeight = buttonSkin.height/4; buttonWidth = buttonSkin.width; var buttonMask:Sprite = new Sprite(); buttonMask.graphics.beginFill(0x000000,1); buttonMask.graphics.drawRect(0,0,buttonWidth,buttonHeight); buttonMask.graphics.endFill(); _this.addChild(buttonMask); buttonSprite.mask = buttonMask; function buttonStageResize (evt:Event) : void { buttonSprite.width = buttonSprite.stage.stageWidth; buttonSprite.height = buttonSprite.stage.stageHeight*4; buttonMask.width = _this.stage.stageWidth; buttonMask.height = _this.stage.stageHeight; }; buttonSprite.width = _this.stage.stageWidth; buttonSprite.height = _this.stage.stageHeight*4; buttonMask.width = _this.stage.stageWidth; buttonMask.height = _this.stage.stageHeight; _this.stage.scaleMode = StageScaleMode.NO_SCALE; _this.stage.align = StageAlign.TOP_LEFT; _this.stage.tabChildren = false; _this.stage.addEventListener(Event.RESIZE, buttonStageResize); _this.addEventListener(MouseEvent.ROLL_OVER, buttonMouseOver); _this.addEventListener(MouseEvent.ROLL_OUT, buttonMouseOut); _this.addEventListener(MouseEvent.MOUSE_DOWN, buttonMouseDown); _this.addEventListener(MouseEvent.MOUSE_UP, buttonMouseUp); _this.addEventListener(MouseEvent.CLICK, handleMouseClick); _this.stage.addEventListener(KeyboardEvent.KEY_DOWN, handleKeyDown); _this.stage.addEventListener(KeyboardEvent.KEY_UP, handleKeyUp); _this.addChild(buttonSprite); } var errorLoader:Function = function (event:IOErrorEvent) : void { renderAsTransparent(); } buttonSkin.contentLoaderInfo.addEventListener(Event.COMPLETE, initLoader); buttonSkin.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, errorLoader); } private function buttonMouseOver (event:MouseEvent) : void { buttonSkin.y = -1*buttonHeight; } private function buttonMouseOut (event:MouseEvent) : void { buttonSkin.y = 0; } private function buttonMouseDown (event:MouseEvent) : void { buttonSkin.y = -2*buttonHeight; } private function buttonMouseUp (event:MouseEvent) : void { buttonSkin.y = 0; } private function renderAsTransparent () : void { function transparentStageResize (evt:Event) : void { buttonSprite.width = buttonSprite.stage.stageWidth; buttonSprite.height = buttonSprite.stage.stageHeight; } buttonSprite.graphics.beginFill(0xffffff, 0); buttonSprite.graphics.drawRect(0,0,5,5); buttonSprite.width = this.stage.stageWidth; buttonSprite.height = this.stage.stageHeight; buttonSprite.graphics.endFill(); this.stage.scaleMode = StageScaleMode.NO_SCALE; this.stage.align = StageAlign.TOP_LEFT; this.stage.tabChildren = false; this.stage.addEventListener(Event.RESIZE, transparentStageResize); this.addEventListener(MouseEvent.CLICK, handleMouseClick); this.addEventListener(MouseEvent.CLICK, transparentClick); this.addEventListener(MouseEvent.MOUSE_DOWN, transparentDown); this.addEventListener(MouseEvent.MOUSE_UP, transparentUp); this.addEventListener(MouseEvent.ROLL_OVER, transparentRollOver); this.addEventListener(MouseEvent.ROLL_OUT, transparentRollOut); this.buttonMode = true; this.useHandCursor = true; this.addChild(buttonSprite); } private function handleKeyDown (evt:KeyboardEvent) : void { if (evt.keyCode == Keyboard.ENTER || evt.keyCode == Keyboard.SPACE) { logMessage("Keyboard 'Enter' or 'Space' down."); buttonSkin.y = -2*buttonHeight; } } private function handleKeyUp (evt:KeyboardEvent) : void { if (evt.keyCode == Keyboard.ENTER || evt.keyCode == Keyboard.SPACE) { buttonSkin.y = 0; logMessage("Keyboard 'Enter' or 'Space' up."); logMessage("Keyboard 'Enter' or 'Space' detected, launching 'Open File' dialog."); this.browse(this.allowMultiple, this.filterArray); } } private function handleFocusIn (evt:FocusEvent) : void { logMessage("Focus is on the Uploader."); } private function handleFocusOut (evt:FocusEvent) : void { logMessage("Focus is out on the Uploader."); } private function handleMouseClick (evt:MouseEvent) : void { logMessage("Mouse click detected, launching 'Open File' dialog."); this.browse(this.allowMultiple, this.filterArray); } //-------------------------------------------------------------------------- // // Overridden Properties // //-------------------------------------------------------------------------- /** * @private * Initializes the component and enables communication with JavaScript * * @param parent A container that the PopUpManager uses to place the Menu * control in. The Menu control may not actually be parented by this object. * * @param xmlDataProvider The data provider for the Menu control. * @see #dataProvider * * @return An instance of the Menu class. * * @see #popUpMenu() * @see com.yahoo.astra.fl.data.XMLDataProvider */ override protected function initializeComponent():void { super.initializeComponent(); var btnSkinURL:String; btnSkinURL = this.stage.loaderInfo.parameters["buttonSkin"]; if (btnSkinURL != null) { this.renderType = "button"; this.renderAsButton(btnSkinURL); } else { this.renderType = "transparent"; this.renderAsTransparent(); } // removeFile (fileID:String = null) // Removes one or all files from the upload queue ExternalInterface.addCallback("removeFile", removeFile); // clearFileList (): Boolean // Clears the list of files to be uploaded. ExternalInterface.addCallback("clearFileList", clearFileList); // upload(fileID:String, url:String, method:String = "GET", vars:Object = null, fieldName:String = "Filedata") // Uploads the specified file in a specified POST variable, attaching other variables using the specified method ExternalInterface.addCallback("upload", upload); // uploadAll(url:String, method:String = "GET", vars:Object = null, fieldName:String = "Filedata") // Uploads all files in the queue, using simultaneousUploads. ExternalInterface.addCallback("uploadAll", uploadAll); // cancel (fileID:String = null) // Cancels the specified file upload; or all, if no id is specified ExternalInterface.addCallback("cancel", cancel); // setAllowLoging (allowLogging:Boolean = false) // Allows log outputs to be produced. ExternalInterface.addCallback("setAllowLogging", setAllowLogging); // setAllowMultipleFiles (allowMultiple:Boolean = false) // Allows multiple file selection ExternalInterface.addCallback("setAllowMultipleFiles", this.setAllowMultipleFiles); // setSimUploadLimit(simUpload:int = [2,5]) // Sets the number of simultaneous uploads allowed when automatically managing queue. ExternalInterface.addCallback("setSimUploadLimit", this.setSimUploadLimit); // setFileFilters(fileFilters:Array) // Sets file filters for file selection. ExternalInterface.addCallback("setFileFilters", this.setFileFilters); // enable() // Enables Uploader UI ExternalInterface.addCallback("enable", enable); // disable() // Disables Uploader UI ExternalInterface.addCallback("disable", disable); // Initialize properties. fileDataList = new Object(); fileRefList = new Object(); fileIDList = new Dictionary(); singleFile = new FileReference(); multipleFiles = new FileReferenceList(); fileIDCounter = 0; filesToUpload = []; } //-------------------------------------- // Private Methods //-------------------------------------- /** * @private * Formats objects containing extensions of files to be filtered into formal FileFilter objects */ private function processFileFilterObjects(filtersArray:Array) : Array { // TODO: Should we have an 'allowedExtensions' property that the JS user accesses directly? Potential here for typos ('extension' instead of 'extensions') as well as a misunderstanding of the nature of the expected array // TODO: Description not showing (testing on OS X PPC player) for (var i:int = 0; i < filtersArray.length; i++) { filtersArray[i] = new FileFilter(filtersArray[i].description, filtersArray[i].extensions, filtersArray[i].macType); } return filtersArray; } /** * @private * Outputs the files selected to an output panel and triggers a 'fileSelect' event. */ private function processSelection():void { var dstring:String = ""; dstring += "Files Selected: \n"; for each (var item:Object in fileDataList) { dstring += item.name + "\n "; } logMessage(dstring); var newEvent:Object = new Object(); newEvent.fileList = fileDataList; newEvent.type = "fileSelect" super.dispatchEventToJavaScript(newEvent); } /** * @private * Adds a file reference object to the internal queue and assigns listeners to its events */ private function addFile(fr:FileReference):void { var fileID:String = "file" + fileIDCounter; var fileName:String = fr.name; var fileCDate:Date = fr.creationDate; var fileMDate:Date = fr.modificationDate; var fileSize:Number = fr.size; fileIDCounter++; fileDataList[fileID] = {id: fileID, name: fileName, cDate: fileCDate, mDate: fileMDate, size: fileSize};//, type: fileType, creator: fileCreator}; fr.addEventListener(Event.OPEN, uploadStart); fr.addEventListener(ProgressEvent.PROGRESS, uploadProgress); fr.addEventListener(Event.COMPLETE, uploadComplete); fr.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, uploadCompleteData); fr.addEventListener(HTTPStatusEvent.HTTP_STATUS, uploadError); fr.addEventListener(IOErrorEvent.IO_ERROR, uploadError); fr.addEventListener(SecurityErrorEvent.SECURITY_ERROR, uploadError); fr.addEventListener(Event.CANCEL,uploadCancel); fileRefList[fileID] = fr; fileIDList[fr] = fileID; } /** * @private * Queues a file for upload */ private function queueForUpload(fr:FileReference, request:URLRequest, fieldName:String):void { filesToUpload.push( {fr:fr, request:request, fieldName:fieldName }); } /** * @private * Uploads the next file in the upload queue. */ private function processQueue():void { while (this.currentUploadThreads < this.simultaneousUploadLimit) { var objToUpload:Object = filesToUpload.pop(); var fr:FileReference = objToUpload.fr; var request:URLRequest = objToUpload.request; var fieldName:String = objToUpload.fieldName; fr.upload(request,fieldName); this.currentUploadThreads++; } } /** * @private * Creates a URLRequest object from a url, and optionally includes an HTTP request method and additional variables to be sent */ private function formURLRequest(url:String, method:String = "GET", vars:Object = null):URLRequest { var request:URLRequest = new URLRequest(); request.url = url; request.method = method; request.data = new URLVariables(); for (var itemName:String in vars) { request.data[itemName] = vars[itemName]; } return request; } /** * @private * Determines whether an object is equivalent to an empty string */ private function isEmptyString(toCheck:*):Boolean { if( toCheck == "null" || toCheck == "" || toCheck == null ) { return true; } else { return false; } } private function logMessage (message:String) : void { if (this.allowLog) { trace(message); ExternalInterface.call("YAHOO.log", message); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -