This is the source code of the “batch-after-batch” multiple file uploader in Flash 8 (ActionScript 2.0) demonstrated at http://demo.programmerspride.com/flash-batch-after-batch-multi-upload – live demo.
import flash.net.FileReference; import flash.net.FileReferenceList; import flash.external.ExternalInterface; var imageFiles:FileReferenceList = new FileReferenceList; var list:Array = new Array(); // Handler to the selected files. var listener:Object = new Object(); // The listener object that listens for FileReference events. var upload_listener:Object = new Object(); imageFiles.addListener(listener); // These next four (4) lines will just load the external "browse button". // Parent movie clip. mc.createEmptyMovieClip("img", getNextHighestDepth()); // Child movie clip that the image will replace. mc.img.createEmptyMovieClip("image", 99); // Create the image loader that loads the "browse button" as configured. var loader:MovieClipLoader = new MovieClipLoader(); loader.loadClip(_root.btn_browse_name, mc.img.image); // When the "browse button" is clicked. mc.onPress = function():Void { // Actual browsing of Files imageFiles.browse([{ description: _root.file_description, extension: _root.file_extension }]); } // When files are selected then call JS UI for enqueueing. listener.onSelect = function():Void { list = imageFiles.fileList; var total:Number = list.length; if(total > 0) { ExternalInterface.call("img.clearqueue()"); for(var i:Number = 0; i<total; i++) { // 0 = Size, 1 = True/False when accepted or not. var info:Array = getFileSize(list[i].size); // Send filename to JS for queueing. ExternalInterface.call("img.enqueue('"+ list[i].name +"','"+ info[0] +"','"+ info[1] +"')"); } } } // Waits for some JS commands from the web-page document. ExternalInterface.addCallback("upload", this, doFileUpload); ExternalInterface.addCallback("cancel", this, doFileCancel); function doFileUpload():Void { var total:Number = list.length; if(total > 0) { for(var i:Number = 0; i<total; i++) { // 0 = Size, 1 = True/False when accepted or not. var info:Array = getFileSize(list[i].size); if(info[1] == true) { list[i].addListener(upload_listener); list[i].upload("upload.php?sessid="+ _root.session_id); } } } else ExternalInterface.call("img.empty()"); } // This function is triggered when an item is cancelled by User. function doFileCancel(filename:String):Void { doRemoval(filename); ExternalInterface.call("img.cancelled('"+ filename +"','"+ ((list.length == 0) ? 1 : 0) +"')"); } function doRemoval(filename:String) { var index:Number = getFileIndex(filename); list[index].cancel(); // Actual cancelling from the memory queue. list[index].removeListener(upload_listener); // Remove attached process handlers. list.splice(index, 1); // Dont' use .pop() in removing an item. Use .splice() instead. } upload_listener.onProgress = function(file:FileReference, bytesLoaded:Number, bytesTotal:Number):Void { var p:Number = Math.round((bytesLoaded / bytesTotal) * 100); var tmp:Array = getFileSize(bytesLoaded); ExternalInterface.call("img.progress('"+ p +"','"+ file.name +"','"+ tmp[0] +"')"); }; upload_listener.onComplete = function(file:FileReference):Void { var filename:String = file.name; doRemoval(filename); ExternalInterface.call("img.uploaded('"+ filename +"','"+ ((list.length == 0) ? 1 : 0) +"')"); }; upload_listener.onHTTPError = function(file:FileReference, httpError:Number):Void { ExternalInterface.call("this.onError('"+ file.name +"','HTTP Error: "+ httpError +"')"); } upload_listener.onIOError = function(file:FileReference):Void { ExternalInterface.call("this.onError('"+ file.name +"','I/O Error')"); } upload_listener.onSecurityError = function(file:FileReference, errorString:String):Void { ExternalInterface.call("this.onError('"+ file.name +"','Security Error: "+ errorString +"')"); }
Initializing the custom “browse” button:
Stage.scaleMode = "noscale"; var canvas_width:Number = 250; var canvas_height:Number = 50; if(_root.btn_browse_width > canvas_width) canvas_width = _root.btn_browse_width; if(_root.btn_browse_height > canvas_height) canvas_height = _root.btn_browse_height; var xPosition:Number = ((canvas_width - _root.btn_browse_width) / 2); var yPosition:Number = ((canvas_height - _root.btn_browse_height) / 2); mc._x = xPosition; mc._y = yPosition;
Miscellaneous:
function getFileIndex(filename:String):Number { var total:Number = list.length; if(total > 0) { for(var i:Number = 0; i<total; i++) { if(list[i].name == filename) { return i; } } } return -1; } function getFileSize(filesize:Number):Array { var bytes:Number = filesize; var unit:String = "B"; var accept:Boolean = true; if(bytes < 1073741824) { // If less than GB // Means MB ( 1024 * 1024 ) if(bytes >= 1048576) { // If greater or equal to MB but less than GB bytes = (bytes / 1024 / 1024); unit = "MB"; } else { // Means KB ( 1024 ) if(bytes >= 1024) { // If greater or equal to KB but less than MB bytes = (bytes / 1024); unit = "KB"; } } } else { // Too large. accept = false; } if(unit != "B") { // Round it off in 2 decimal places bytes = (Math.round(bytes * Math.pow(10, 2)) / Math.pow(10, 2)) } if(bytes > _root.file_size_max && unit == "MB") accept = false; return [bytes +" "+ unit, accept]; }
Advertisements