Is there a way how to check if UPLOAD_COMPLETE_DATA
event was not dispatched after COMPLETE
in Flash?
I'm working on file uploader. It uploads file after file – after COMPLETE
next file starts uploading – this is repeated for every file. On last file on COMPLETE
if there is no next file allCompleteHandler is dispatched and if some errors occurs it shows that errors at once trough javascript to the user.
I handle that errors in javascript – it stores every error to variable in javascript and after all completed it shows that errors.
The problem is:
I can't store error from the last file (error from server that I get through UPLOAD_COMPLETE_DATA
) because this is dispatched after COMPLETE
(after allCompleteHandler).
I need to solve this because I don't wont to show javascript alert box for every invalid file (if there are 100 files for example).
var parameters:Object = LoaderInfo(this.root.loaderInfo).parameters,
fileFilter:Array,
browseFilter:Array = [],
files:FileReferenceList = new FileReferenceList(),
selectedFiles:Array = [],
file:FileReference = new FileReference(),
url:URLRequest = new URLRequest(parameters.phpScript),
uploadFolder,
postMaxSize:Number,
uploadMaxFilesize:Number,
maxInputTime:int,
speedTimer:Timer = new Timer(1000),
uploadTimer:Timer = new Timer(60000, 1),
count:int = 0,
totalBytes:Number = 0,
processedBytes:Number = 0,
currentBytes:Number = 0,
currentBytes2:Number = 0,
lastBytes:Number = 0,
uploadSpeed:Number = 0,
inProgress:Boolean = false;
// Browse filter setup
fileFilter = ExternalInterface.call(parameters.fileManager + ".getFileFilter");
if (fileFilter) {
for (var i:int = 0; i < fileFilter.length; i++) {
browseFilter.push(new FileFilter(fileFilter[i][0], fileFilter[i][1]));
}
}
function clickHandler(event:MouseEvent):void {
if (!inProgress) {
uploadFolder = ExternalInterface.call(parameters.fileManager + ".getCurrentFolder");
if (uploadFolder != undefined) {
files.browse(browseFilter);
}
}
}
stage.addEventListener(MouseEvent.CLICK, clickHandler);
function selectHandler(event:Event):void {
var variables:URLVariables = new URLVariables();
variables.folder = uploadFolder;
url.data = variables;
url.method = URLRequestMethod.POST;
selectedFiles = files.fileList;
postMaxSize = ExternalInterface.call(parameters.fileManager + ".getPostMaxSize");
postMaxSize = postMaxSize ? postMaxSize : 50 * 1024 * 1024;
uploadMaxFilesize = ExternalInterface.call(parameters.fileManager + ".getUploadMaxFilesize");
uploadMaxFilesize = uploadMaxFilesize ? uploadMaxFilesize : 50 * 1024 * 1024;
maxInputTime = ExternalInterface.call(parameters.fileManager + ".getMaxInputTime");
maxInputTime = maxInputTime ? maxInputTime : 60;
// Get total size of selected files
for (var i:int = 0; i < selectedFiles.length; i++) {
totalBytes += selectedFiles[i].size;
}
ExternalInterface.call(parameters.fileManager + ".selectHandler", {
selectedFiles : selectedFiles,
totalBytes : totalBytes
});
// Start upload process
inProgress = true;
speedTimer.start();
upload();
}
files.addEventListener(Event.SELECT, selectHandler);
function upload():void {
uploadTimer.reset();
currentBytes2 = 0;
if (count) {
processedBytes += file.size;
if (currentBytes < processedBytes) {
currentBytes = processedBytes;
}
}
if (selectedFiles.length) {
file = FileReference(selectedFiles.shift());
count++;
ExternalInterface.call(parameters.fileManager + ".beforeUploadHandler", {
file : file,
currentBytes : currentBytes
});
if (file.size <= postMaxSize) {
if (file.size <= uploadMaxFilesize) {
file.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
file.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
file.addEventListener(Event.OPEN, openHandler);
file.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
file.addEventListener(ProgressEvent.PROGRESS, progressHandler);
file.addEventListener(Event.COMPLETE, completeHandler);
file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, uploadCompleteDataHandler);
file.upload(url);
} else {
ExternalInterface.call(parameters.fileManager + ".uploadMaxFilesizeHandler", file);
upload();
}
} else {
ExternalInterface.call(parameters.fileManager + ".postMaxSizeHandler", file);
upload();
}
} else {
ExternalInterface.call(parameters.fileManager + ".allCompleteHandler", {
currentBytes : currentBytes
});
speedTimer.stop();
count = 0;
totalBytes = 0;
processedBytes = 0;
currentBytes = 0;
lastBytes = 0;
uploadSpeed = 0;
inProgress = false;
}
}
function securityErrorHandler(event:SecurityErrorEvent):void {
ExternalInterface.call(parameters.fileManager + ".securityErrorHandler", event);
}
function httpStatusHandler(event:HTTPStatusEvent):void {
ExternalInterface.call(parameters.fileManager + ".httpStatusHandler", event);
selectedFiles = [];
}
function openHandler(event:Event):void {
ExternalInterface.call(parameters.fileManager + ".openHandler", event);
uploadTimer.delay = maxInputTime * 1000;
uploadTimer.start();
}
function ioErrorHandler(event:IOErrorEvent):void {
ExternalInterface.call(parameters.fileManager + ".ioErrorHandler", event);
upload();
}
function progressHandler(event:ProgressEvent):void {
currentBytes += event.bytesLoaded - currentBytes2;
currentBytes2 = event.bytesLoaded;
ExternalInterface.call(parameters.fileManager + ".progressHandler", {
current : event,
currentBytes : currentBytes
});
}
function completeHandler(event:Event):void {
ExternalInterface.call(parameters.fileManager + ".completeHandler", event);
upload();
}
function uploadCompleteDataHandler(event:DataEvent):void {
ExternalInterface.call(parameters.fileManager + ".uploadCompleteDataHandler", "(" + event.data + ")");
}
function updateUploadSpeed(event:TimerEvent):void {
if (currentBytes > lastBytes) {
uploadSpeed = currentBytes - lastBytes;
ExternalInterface.call(parameters.fileManager + ".uploadSpeedHandler", uploadSpeed);
lastBytes = currentBytes;
}
}
speedTimer.addEventListener(TimerEvent.TIMER, updateUploadSpeed);
function maxInputTimeHandler(event:TimerEvent):void {
ExternalInterface.call(parameters.fileManager + ".maxInputTimeHandler", file);
}
uploadTimer.addEventListener(TimerEvent.TIMER, maxInputTimeHandler);
function cancelUpload():void {
file.cancel();
selectedFiles = [];
upload();
}
ExternalInterface.addCallback("cancelUpload", cancelUpload);
I can do this by setting up my PHP script to always return data and check this data and start next file upload with UPLOAD_COMPLETE_DATA
, but I don't like this (it can be slow maybe I think)...
It is simple question but maybe hard to explain why I need that. Thank you for your help!