Skip to content

Instantly share code, notes, and snippets.

@sundayu
Created May 12, 2010 17:34
Show Gist options
  • Save sundayu/398867 to your computer and use it in GitHub Desktop.
Save sundayu/398867 to your computer and use it in GitHub Desktop.
package {
import flash.events.EventDispatcher;
import flash.net.FileReference;
import flash.events.*;
import flash.external.ExternalInterface;
import ExternalCall;
internal class FileItem
{
private static var file_id_count:Number = 0; // tracks the file id sequence
private var js_object:Object;
public var file_reference:FileReference;
public var id:String;
public var index:Number = -1;
public var file_status:Number = 0;
//callbacks
private var uploadProgress_Callback:String;
private var uploadSuccess_Callback:String;
//file status
public static var FILE_STATUS_QUEUED:int = -1;
public static var FILE_STATUS_IN_PROGRESS:int = -2;
public static var FILE_STATUS_ERROR:int = -3;
public static var FILE_STATUS_SUCCESS:int = -4;
public static var FILE_STATUS_CANCELLED:int = -5;
public static var FILE_STATUS_NEW:int = -6; // This file status should never be sent to JavaScript
public static var UPLOAD_TYPE_NORMAL:int = -1;
public static var UPLOAD_TYPE_RESIZE:int = -2;
public function FileItem(file_reference:FileReference, control_id:String, index:Number)
{
this.file_reference = file_reference;
this.id = control_id + "__" + (FileItem.file_id_count++);
this.index = index;
this.js_object = {
id: this.id,
index: this.index,
post: this.GetPostObject()
};
// Cleanly attempt to retrieve the FileReference info
// this can fail and so is wrapped in try..catch
try {
this.js_object.name = this.file_reference.name;
this.js_object.size = this.file_reference.size;
this.js_object.type = this.file_reference.type || "";
this.js_object.creationdate = this.file_reference.creationDate || new Date(0);
this.js_object.modificationdate = this.file_reference.modificationDate || new Date(0);
} catch (ex:Error) {
this.file_status = FileItem.FILE_STATUS_ERROR;
}
}
public function GetUploader():EventDispatcher {
return this.file_reference;
}
public function AddParam(name:String, value:String):void {
this.postObject[name] = value;
}
public function RemoveParam(name:String):void {
delete this.postObject[name];
}
// Create the simply file object that is passed to the browser
public function ToJavaScriptObject():Object {
this.js_object.filestatus = this.file_status;
this.js_object.post = this.GetPostObject(true);
return this.js_object;
}
public function toString():String {
return "FileItem - ID: " + this.id;
}
//file processing and handing functions
public function StartUpload(request:URLRequest, filePostName:String):void {
this.file_status = FileItem.FILE_STATUS_IN_PROGRESS;
this.file_reference.upload(request, filePostName);
}
//file upload handler
public function FileProgress_Handler(event:ProgressEvent):void {
var bytesLoaded:Number = event.bytesLoaded < 0 ? 0 : event.bytesLoaded;
var bytesTotal:Number = event.bytesTotal < 0 ? 0 : event.bytesTotal;
ExternalCall.UploadProgress(this.uploadProgress_Callback, this.ToJavaScriptObject(), bytesLoaded, bytesTotal);
}
public function setUploadProgress_Callback(uploadProgress_Callback:String):void {
this.uploadProgress_Callback = uploadProgress_Callback;
}
public function Complete_Handler(event:ProgressEvent):void {
ExternalInterface.call("debug", "11");
}
public function setComplete_Handler(Complete_Handler:String):void {
}
public function UploadSuccess(file:FileItem, serverData:String, responseReceived:Boolean = true):void {
ExternalCall.UploadSuccess(this.uploadSuccess_Callback, file.ToJavaScriptObject(), serverData, responseReceived);
}
public function ServerData_Handler(event:DataEvent):void {
this.UploadSuccess(this, event.data);
}
public function setUploadSuccess_Callback(uploadSuccess_Callback:String):void {
this.uploadSuccess_Callback = uploadSuccess_Callback;
}
private function addEventListeners():void {
var item:EventDispatcher = this.GetUploader();
if (item != null) {
//item.addEventListener(Event.OPEN, this.Open_Handler);
item.addEventListener(ProgressEvent.PROGRESS, this.FileProgress_Handler);
//item.addEventListener(IOErrorEvent.IO_ERROR, this.IOError_Handler);
//item.addEventListener(SecurityErrorEvent.SECURITY_ERROR, this.SecurityError_Handler);
//item.addEventListener(HTTPStatusEvent.HTTP_STATUS, this.HTTPError_Handler);
item.addEventListener(Event.COMPLETE, this.Complete_Handler);
item.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, this.ServerData_Handler);
}
}
private function removeEventListeners():void {
var item:EventDispatcher = this.GetUploader();
if (item != null) {
//item.removeEventListener(Event.OPEN, this.Open_Handler);
item.removeEventListener(ProgressEvent.PROGRESS, this.FileProgress_Handler);
//item.removeEventListener(IOErrorEvent.IO_ERROR, this.IOError_Handler);
//item.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, this.SecurityError_Handler);
//item.removeEventListener(HTTPStatusEvent.HTTP_STATUS, this.HTTPError_Handler);
item.removeEventListener(Event.COMPLETE, this.Complete_Handler);
item.removeEventListener(DataEvent.UPLOAD_COMPLETE_DATA, this.ServerData_Handler);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment