Cancel a POST request - actionscript-3

I have an app that makes a request to a server to generate an audio file and the problem that I have is that if I send two distinct and consecutive requests, I can not cancel the first. I tried with .unload or .unloadAndStop but always gives me error.
Any suggestions?
elfich="sonido" + elfichnum;
var request:URLRequest = new URLRequest();
var variables:URLVariables = new URLVariables();
var loader:URLLoader = new URLLoader();
variables.text=caja_texto;
variables.fileName=elfich;
request.url = "http://192.168.0.19:800/cgi-bin/tts.cgi";
request.method = URLRequestMethod.POST;
request.data = variables;
loader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE, cargacompletanew);
loader.addEventListener(IOErrorEvent.IO_ERROR, onErrornew);
loader.load(request);
}
function cargacompletanew(event:Event):void
{
xlocalSound = new Sound();
var xreq:URLRequest = new URLRequest("http://192.168.0.19:800/output/" + elfich + ".mp3");
xlocalSound.load(xreq);
xlocalSound.addEventListener(Event.OPEN, iniciarSonido);
}

What about
loader.removeEventListener(Event.COMPLETE, cargacompletanew);
loader.removeEventListener(IOErrorEvent.IO_ERROR, onErrornew);
loader.close();
I think there was a problem with close() if you were trying to close it if nothing was loaded, so I'd try to put that inn try/catch block.
try
{
loader.close();
}
catch(e:Error)
{
loader("Oh noes!");
}

Related

AS3 - navigateToURL issue

I have below code:
botton_1.addEventListener(MouseEvent.MOUSE_UP, onClick);
function onClick(e:MouseEvent):void {
var url:String = "http://www.xxxxxxxxxx.com/signInDetail";
var request:URLRequest = new URLRequest(url);
request.method = URLRequestMethod.POST;
var variables:URLVariables = new URLVariables();
variables.query = ta.text;
variables.packageId = 1;
variables.update = "askADoctorSaveQuery";
variables.task = "CHAT_PRE_ACTIONS_WITHOUT_PACKAGEID";
variables.query = ta.text;
request.data = variables;
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE,compleHandler);
loader.load(request);
}
function compleHandler(event:Event):void {
var click_url2:String = "http://www.xxxxxxxx.com/ask-doctor-online";
var request2:URLRequest = new URLRequest(click_url2);
navigateToURL(request2, "_blank");
}
After the first request is loaded and compleHandler is called, the navigateToURL method opens the url in a popup instead of new tab. I am not sure what is the issue.
PLease help...
Replace this line. This loads it into a new window.
navigateToURL(request2, "_blank");
You have two other options:
In parent frame :
navigateToURL(request2, "_parent");
Replace current page :
navigateToURL(request2, "_self");
EDIT
After rechecking, I believe opening a window in a new tab is a user preference for the browser. There seems to be no way that a developer can control this. In IE 8 for example:
http://www.computershopper.com/feature/50-windows-7-tips-tweaks-and-secrets/open-a-link-in-a-new-tab-in-internet-explorer-8
navigateToURL(request2, "_self");

Sending AS3 variables to and email using PHP

I'm working on a flash content on my website that contains an input box and a submit button. The user should put an answer of a question in the input box and when he clicks on submit, the answer should be sent to an email address. The problem is, when a user enters an answer, I receive an email that contains :
array (
)
. Here are my codes:
AS3 Code:
var myData:URLVariables = new URLVariables();
myData.answer = answer.text;
var myRequest:URLRequest = new URLRequest("example.php");
myRequest.data = myData;
myRequest.method = URLRequestMethod.POST;
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
The button's code:
myButton.addEventListener(MouseEvent.CLICK, sen) ;
function sen(Event:MouseEvent):void
{
navigateToURL( new URLRequest("example.php"), "_self");
}
The PHP code:
<?php
$text = var_export($_POST, true);
$to = "webhosting4#outlook.com";
$subject="Message from php";
mail($to,$subject,$text,"Content-Type: text/plain; charset=utf-8");
?>
So, what am I doing wrong?
myButton.addEventListener(MouseEvent.CLICK, sen) ;
function sen(Event:MouseEvent):void
{
var myData:URLVariables = new URLVariables();
myData.answer = answer.text;
var myRequest:URLRequest = new URLRequest("example.php");
myRequest.data = myData;
myRequest.method = URLRequestMethod.POST;
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.load( myRequest );
};

Sending data from flash to php using post method as2

I am using following code in as3:
var Request:URLRequest = new URLRequest();
var xmlMain:XML=new XML();
var urlXmlLoader:Loader = new Loader();
var myVars:URLVariables = new URLVariables();
myVars.RequestXml = xmlMain;
Request.url = "http://www.xyz.php"; //some php url
Request.method = URLRequestMethod.POST;
Request.data = myVars;
urlXmlLoader.dataFormat = URLLoaderDataFormat.TEXT;
urlXmlLoader.addEventListener(Event.COMPLETE, getData);
urlXmlLoader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
urlXmlLoader.load(Request);
Now I have an as2 project where I need to do the same. How can it be done in as2??
This is probably the best tutorial on HTTP requests in AS2: http://www.sephiroth.it/tutorials/flashPHP/loadVars/

AS3/AIR: IOStream Error when trying to upload files

I'm making an AIR app in which the user drags and drops image files onto the stage, the app then uploads those dropped images to the site imgur.com, and then returns with links to the images (in XML format).
I am having two issues:
the program works successfully with a very small file, however anything larger (1mb or more), I get an IOStream error 2023. Imgur supports uploads of up to 10mb so I know it must be something on my end.
I would like to be able to drop multiple image files at once into the program, and have it upload all of them (either synchronously or asynchronously, it does not matter). Right now the program gives errors when I attempt to do this (something along the lines of the byteArray having to be greater than 0).
Below is the code in question which is giving me issues. (thank you for your help in advanced)
private function doDragDrop(e:NativeDragEvent):void
{
trace("doDragDrop() called.");
var dropFiles:Array = e.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;
for each (var file:File in dropFiles)
{
switch (file.extension.toLowerCase())
{
case "jpg" :
case "jpeg" :
case "gif" :
case "png" :
case "apng" :
case "tiff" :
case "bmp" :
case "pdf" :
case "xcf" :
trace("file Extension Check = passed");
addImage(file.nativePath);
break;
//error handling for non-supported filetypes
default :
trace("ERROR: Unsupported File Type Detected!");
}
}
}
private function addImage(nativePath:String):void
{
trace("addImage() called.");
trace("NativePath is: " + nativePath);
//var file:File = new File(nativePath);
var file:File = File.desktopDirectory.resolvePath(nativePath);
var ba:ByteArray = new ByteArray();
var stream:FileStream = new FileStream();
stream.open(file, FileMode.READ);
stream.readBytes(ba);
stream.close();
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, fileLoaded);
loader.loadBytes(ba);
}
private function fileLoaded(e:Event):void
{
trace("fileLoaded() called.");
var bitmap:Bitmap = Bitmap(e.target.content);
var bitmapData:BitmapData = bitmap.bitmapData;
var png:ByteArray = PNGEncoder.encode(bitmapData);
urlLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
urlLoader.addEventListener(Event.COMPLETE, onCookieSent);
urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
var vars:String = "?key=" + API_KEY + "&name=name&title=title";
var request:URLRequest = new URLRequest(UPLOAD_URL + vars);
request.contentType = "application/octet-stream";
request.method = URLRequestMethod.POST;
request.data = png;
urlLoader.load(request);
}
private function onCookieSent(e:Event):void
{
trace("onCookieSent() called.");
var res:XML = new XML(unescape(urlLoader.data));
var resultsList:XMLList = res.links;
trace(resultsList);
}
private function onIOError(e:IOErrorEvent):void
{
trace("ioErrorHandler: " + e);
TweenLite.to(tv_mc, 2, {alpha:0.5, scaleX:1.0, scaleY:1.0, ease:Elastic.easeOut});
// Handle error
}
private function onSecurityError(e:SecurityErrorEvent):void
{
trace("securityErrorHandler: " + e);
TweenLite.to(tv_mc, 2, {alpha:0.5, scaleX:1.0, scaleY:1.0, ease:Elastic.easeOut});
// handle error
}
//When the dragged object leaves the drop point, do this
private function doDragExit(e:NativeDragEvent):void
{
trace("doDragExit() called.");
TweenLite.to(tv_mc, 2, {alpha:0.5, scaleX:1.0, scaleY:1.0, ease:Elastic.easeOut});
}
after doing this:
var ba:ByteArray = new ByteArray();
var stream:FileStream = new FileStream();
stream.open(file, FileMode.READ);
stream.readBytes(ba);
stream.close();
you already have a ByteArray of the file/image. can't you just send this one...
private function addImage(nativePath:String):void
{
trace("addImage() called.");
trace("NativePath is: " + nativePath);
//var file:File = new File(nativePath);
var file:File = File.desktopDirectory.resolvePath(nativePath);
var ba:ByteArray = new ByteArray();
var stream:FileStream = new FileStream();
stream.open(file, FileMode.READ);
stream.readBytes(ba);
stream.close();
urlLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
urlLoader.addEventListener(Event.COMPLETE, onCookieSent);
urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
var vars:String = "?key=" + API_KEY + "&name=name&title=title";
var request:URLRequest = new URLRequest(UPLOAD_URL + vars);
request.contentType = "application/octet-stream";
request.method = URLRequestMethod.POST;
request.data = ba;
urlLoader.load(request);
}

AS3 - Remove Child Problem

I'm using the following code to display an advertisement in a mobile game that I'm working on. It works fine but I don't know what to put in the clickStart function to remove the advertisement from the stage before the game plays. I've been playing around with removeChild but can't seem to get it to work.
stop();
startButton.addEventListener(MouseEvent.CLICK,clickStart);
function clickStart(event:MouseEvent) {
gotoAndStop("play");
}
var request:URLRequest = new URLRequest("http://soma.smaato.com/oapi/reqAd.jsp");
var variables:URLVariables = new URLVariables();
variables.adspace = "0";
variables.pub = "0";
variables.devip = "127.0.0.1";
variables.format = "IMG";
variables.adcount = "1";
variables.response = "XML";
request.data = variables;
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, onComplete);
loader.load(request);
function onComplete(e:Event):void
{
var data:XML = new XML(loader.data as String);
var status:String = data.*::status.toString();
if(status == "success")
{
var ad:XMLList = data.*::ads.*::ad;
var link:String = ad.*::link.toString();
var l:Loader = new Loader();
l.load(new URLRequest(link));
addChild(l);
l.x = 135;
l.y = 265;
var clickurl:String = ad.*::action.#target.toString();
l.addEventListener(MouseEvent.CLICK, onAdClick);
function onAdClick(e:MouseEvent):void
{
var request:URLRequest = new URLRequest(clickurl);
navigateToURL(request);
}
}
}
You need to move the declaration of the Loader outside of the onComplete function, since the variable runs out of scope.
var l:Loader = new Loader();
You could try placing it on the same scope as these:
var request:URLRequest = new URLRequest("http://soma.smaato.com/oapi/reqAd.jsp");
var variables:URLVariables = new URLVariables();
then you should be able to update your clickStart function to something like this:
function clickStart(event:MouseEvent) {
removeChild(l);
gotoAndStop("play");
}