📄 multifileupload.as
字号:
_file = FileReference(_files.getItemAt(0));
_file.addEventListener(Event.OPEN, openHandler);
_file.addEventListener(ProgressEvent.PROGRESS, progressHandler);
_file.addEventListener(Event.COMPLETE, completeHandler);
_file.addEventListener(SecurityErrorEvent.SECURITY_ERROR,securityErrorHandler);
_file.addEventListener(HTTPStatusEvent.HTTP_STATUS,httpStatusHandler);
_file.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
_file.upload(_uploadURL);
setupCancelButton(true);
}
}
//Remove Selected File From Cue
private function removeSelectedFileFromCue(event:Event):void{
if (_datagrid.selectedIndex >= 0){
_files.removeItemAt( _datagrid.selectedIndex);
}
}
//Remove all files from the upload cue;
private function clearFileCue(event:Event):void{
_files.removeAll();
}
// Cancel Current File Upload
private function cancelFileIO(event:Event):void{
_file.cancel();
setupCancelButton(false);
checkCue();
}
//label function for the datagird File Size Column
private function bytesToKilobytes(data:Object,blank:Object):String {
var kilobytes:String;
kilobytes = String(Math.round(data.size/ 1024)) + ' kb';
return kilobytes
}
// Feed the progress bar a meaningful label
private function getByteCount():void{
var i:int;
_totalbytes = 0;
for(i=0;i < _files.length;i++){
_totalbytes += _files[i].size;
}
_progressbar.label = "Total Files: "+ _files.length+ " Total Size: " + Math.round(_totalbytes/1024) + " kb"
}
// Checks the files do not exceed maxFileSize | if _maxFileSize == 0 No File Limit Set
private function checkFileSize(filesize:Number):Boolean{
var r:Boolean = false;
//if filesize greater then _maxFileSize
if (filesize > _maxFileSize){
r = false;
trace("false");
}else if (filesize <= _maxFileSize){
r = true;
trace("true");
}
if (_maxFileSize == 0){
r = true;
}
return r;
}
// restores progress bar back to normal
private function resetProgressBar():void{
_progressbar.label = "";
_progressbar.maximum = 0;
_progressbar.minimum = 0;
}
// reset form item elements
private function resetForm():void{
_uploadbutton.enabled = false;
_uploadbutton.addEventListener(MouseEvent.CLICK,uploadFiles);
_uploadbutton.label = "Upload";
_progressbar.maximum = 0;
_totalbytes = 0;
_progressbar.label = "";
_remselbutton.enabled = false;
_remallbutton.enabled = false;
_browsebutton.enabled = true;
}
// whenever the _files arraycollection changes this function is called to make sure the datagrid data jives
private function popDataGrid(event:CollectionEvent):void{
getByteCount();
checkCue();
}
// enable or disable upload and remove controls based on files in the cue;
private function checkCue():void{
if (_files.length > 0){
_uploadbutton.enabled = true;
_remselbutton.enabled = true;
_remallbutton.enabled = true;
}else{
resetProgressBar();
_uploadbutton.enabled = false;
}
}
// toggle upload button label and function to trigger file uploading or upload cancelling
private function setupCancelButton(x:Boolean):void{
if (x == true){
_uploadbutton.label = "Cancel";
_browsebutton.enabled = false;
_remselbutton.enabled = false;
_remallbutton.enabled = false;
_uploadbutton.addEventListener(MouseEvent.CLICK,cancelFileIO);
}else if (x == false){
_uploadbutton.removeEventListener(MouseEvent.CLICK,cancelFileIO);
resetForm();
}
}
/*********************************************************
* File IO Event Handlers *
*********************************************************/
// called after user selected files form the browse dialouge box.
private function selectHandler(event:Event):void {
var i:int;
var msg:String ="";
var dl:Array = [];
for (i=0;i < event.currentTarget.fileList.length; i ++){
if (checkFileSize(event.currentTarget.fileList[i].size)){
_files.addItem(event.currentTarget.fileList[i]);
trace("under size " + event.currentTarget.fileList[i].size);
} else {
dl.push(event.currentTarget.fileList[i]);
trace(event.currentTarget.fileList[i].name + " too large");
}
}
if (dl.length > 0){
for (i=0;i<dl.length;i++){
msg += String(dl[i].name + " is too large. \n");
}
mx.controls.Alert.show(msg + "Max File Size is: " + Math.round(_maxFileSize / 1024) + " kb","File Too Large",4,null).clipContent;
}
}
// called after the file is opened before upload
private function openHandler(event:Event):void{
trace('openHandler triggered');
_files;
}
// called during the file upload of each file being uploaded | we use this to feed the progress bar its data
private function progressHandler(event:ProgressEvent):void {
_progressbar.setProgress(event.bytesLoaded,event.bytesTotal);
_progressbar.label = "Uploading " + Math.round(event.bytesLoaded / 1024) + " kb of " + Math.round(event.bytesTotal / 1024) + " kb " + (_files.length - 1) + " files remaining";
}
// called after a file has been successully uploaded | we use this as well to check if there are any files left to upload and how to handle it
private function completeHandler(event:Event):void{
//trace('completeHanderl triggered');
_files.removeItemAt(0);
if (_files.length > 0){
_totalbytes = 0;
uploadFiles(null);
}else{
setupCancelButton(false);
_progressbar.label = "Uploads Complete";
var uploadCompleted:Event = new Event(Event.COMPLETE);
dispatchEvent(uploadCompleted);
}
}
// only called if there is an error detected by flash player browsing or uploading a file
private function ioErrorHandler(event:IOErrorEvent):void{
//trace('And IO Error has occured:' + event);
mx.controls.Alert.show(String(event),"ioError",0);
}
// only called if a security error detected by flash player such as a sandbox violation
private function securityErrorHandler(event:SecurityErrorEvent):void{
//trace("securityErrorHandler: " + event);
mx.controls.Alert.show(String(event),"Security Error",0);
}
// This function its not required
private function cancelHandler(event:Event):void{
// cancel button has been clicked;
trace('cancelled');
}
// after a file upload is complete or attemted the server will return an http status code, code 200 means all is good anything else is bad.
private function httpStatusHandler(event:HTTPStatusEvent):void {
// trace("httpStatusHandler: " + event);
if (event.status != 200){
mx.controls.Alert.show(String(event),"Error",0);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -