Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
215 lines (187 sloc) 9.11 KB
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" backgroundAlpha="0" creationComplete="registerCallbacks();" layout="absolute">
<mx:Script>
<![CDATA[
import flash.external.ExternalInterface;
import flash.net.FileReference;
import com.adobe.net.MimeTypeMap;
import com.elctech.S3UploadOptions;
import com.elctech.S3UploadRequest;
private var mimeMap:MimeTypeMap = new MimeTypeMap();
private var options:S3UploadOptions = new S3UploadOptions();
private var fileReference:FileReference;
private function registerCallbacks():void {
if (ExternalInterface.available) {
ExternalInterface.addCallback("init", init);
ExternalInterface.addCallback("upload", upload);
ExternalInterface.call('s3_swf.init');
}
}
private function init(url:String, initialMessage:String="mxml", doChecks:String="0",
onSuccess:String="", onFailed:String="",
onSelected:String="", onCancel:String=""
):void {
options.SignatureQueryURL = url;
userMessage.text = initialMessage;
options.doChecks = doChecks;
if (onSuccess != "") { options.onSuccessCall = onSuccess; }
if (onFailed != "") { options.onFailedCall = onFailed; }
if (onSelected != "") { options.onSelectedCall = onSelected; }
if (onCancel != "") { options.onCancelCall = onCancel; }
}
private function browser():void {
this.fileReference = new FileReference();
userMessage.setStyle("color","#222222");
// setup file reference event handlers
fileReference.addEventListener(Event.CANCEL, function(event:Event):void {
// TODO: cancel the upload.
});
fileReference.addEventListener(Event.SELECT, function(event:Event):void {
// set options.FileName
options.FileName = fileReference.name.replace(/^.*(\\|\/)/gi, '').replace(/[^A-Za-z0-9\.\-]/gi, '_');
userMessage.text = options.FileName;
// set options:FileSize
options.FileSize = fileReference.size.toString();
// set options.ContentType
var FileNameArray:Array = options.FileName.split(/\./);
var FileExtension:String = FileNameArray[FileNameArray.length - 1];
options.ContentType = mimeMap.getMimeType(FileExtension);
trace(options.onSelectedCall);
ExternalInterface.call(options.onSelectedCall, options.FileName, options.FileSize, options.ContentType);
});
fileReference.browse();
}
private function upload(prefixPath:String = ""):void {
// return if user has not selected any file
if (options.FileSize == null) {
userMessage.text = 'You need to select a file!';
trace(options.onFailedCall);
ExternalInterface.call(options.onFailedCall);
return;
}
userMessage.text = 'Initiating...';
// ======================== //
// Start Query S3 Signature //
// ======================== //
var request:URLRequest = new URLRequest(options.SignatureQueryURL);
var loader:URLLoader = new URLLoader();
var variables:URLVariables = new URLVariables();
options.PrefixPath = prefixPath; // reset options.PrefixPath
options.key = options.PrefixPath + options.FileName;
variables.file_name = options.FileName;
variables.file_size = options.FileSize;
variables.key = options.key;
variables.content_type = options.ContentType;
variables.do_checks = options.doChecks;
request.method = URLRequestMethod.GET;
request.data = variables;
loader.dataFormat = URLLoaderDataFormat.TEXT;
configureListeners(loader);
loader.load(request);
}
private function configureListeners(dispatcher:IEventDispatcher):void {
dispatcher.addEventListener(Event.COMPLETE, completeHandler);
dispatcher.addEventListener(Event.OPEN, openHandler);
dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
}
private function completeHandler(event:Event):void {
var loader:URLLoader = URLLoader(event.target);
var xml:XML = new XML(loader.data);
options.policy = xml.policy;
options.signature = xml.signature;
options.bucket = xml.bucket;
options.AWSAccessKeyId = xml.accesskeyid;
options.acl = xml.acl;
options.Expires = xml.expirationdate;
options.Secure = xml.https;
if (xml.errorMessage!="")
{
uploadProgressBar.visible = false;
selectButton.visible = true;
userMessage.visible = true;
userMessage.text = "Error: " + xml.errorMessage;
userMessage.setStyle("color","#770000");
ExternalInterface.call(options.onFailedCall);
return;
}
// ===================== //
// Start post file to S3 //
// ===================== //
var request:S3UploadRequest = new S3UploadRequest(options);
request.addEventListener(Event.OPEN, function(event:Event):void {
userMessage.text = "";
uploadProgressBar.visible = true;
selectButton.visible = false;
trace(event);
});
request.addEventListener(ProgressEvent.PROGRESS, function(event:ProgressEvent):void {
var penct:uint = uint(event.bytesLoaded / event.bytesTotal * 100);
uploadProgressBar.label = 'Uploading ' + penct + " %";
uploadProgressBar.setProgress(event.bytesLoaded, event.bytesTotal);
});
request.addEventListener(IOErrorEvent.IO_ERROR, function(event:IOErrorEvent):void {
uploadProgressBar.visible = false;
selectButton.visible = true;
userMessage.text = "Upload Error, please retry.";
trace(options.onFailedCall);
ExternalInterface.call(options.onFailedCall);
trace(event);
});
request.addEventListener(SecurityErrorEvent.SECURITY_ERROR, function(event:SecurityErrorEvent):void {
uploadProgressBar.visible = false;
selectButton.visible = true;
userMessage.text = "Upload error, Access denied.";
trace(options.onFailedCall);
ExternalInterface.call(options.onFailedCall);
trace(event);
});
request.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, function(event:Event):void {
uploadProgressBar.visible = false;
//selectButton.visible = true;
userMessage.text = "Upload complete!";
trace(options.onSuccessCall);
ExternalInterface.call(options.onSuccessCall, options.FileName, options.FileSize, options.ContentType);
trace(event);
});
try {
userMessage.text = "Uploading commenced...";
request.upload(fileReference);
} catch(e:Error) {
uploadProgressBar.visible = false;
selectButton.visible = true;
userMessage.text = "" + e;
userMessage.setStyle("color","#770000");
trace("An error occurred: " + e);
}
}
private function openHandler(event:Event):void {
userMessage.text = "Preparing for upload...";
trace("openHandler: " + event);
}
private function progressHandler(event:ProgressEvent):void {
trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
}
private function securityErrorHandler(event:SecurityErrorEvent):void {
trace("securityErrorHandler: " + event);
trace(options.onFailedCall);
userMessage.text = "Whoa - something went really wrong preparing for upload: securityErrorHandler: " + event;
ExternalInterface.call(options.onFailedCall);
}
private function httpStatusHandler(event:HTTPStatusEvent):void {
trace("httpStatusHandler: " + event);
}
private function ioErrorHandler(event:IOErrorEvent):void {
trace("ioErrorHandler: " + event);
trace(options.onFailedCall);
userMessage.text = "Whoa - something went really wrong preparing for upload: ioErrorHandler: " + event;
ExternalInterface.call(options.onFailedCall);
}
]]>
</mx:Script>
<mx:Label x="100" y="10" id="userMessage" color="#222222" fontSize="12" text=""/>
<mx:Button x="10" y="7" label="Browse..." click="browser()" id="selectButton"/>
<mx:ProgressBar x="1" y="7" width="486" height="20" labelPlacement="center" id="uploadProgressBar" visible="false" mode="manual" label="Uploading..."/>
</mx:Application>
Something went wrong with that request. Please try again.