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");
Related
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!");
}
I have the following code in my AS3 Flash code that takes a screenshot within the swf using JPGEncoder and sends it to the url where i write it to a file in PHP.
I did run into the Google Chrome Pepperflash issue recently where the function just stops and the page fails to redirect. Nothing gets sent to save.php. By changing
var header:URLRequestHeader = new URLRequestHeader ("Content-type", "application/octet-stream");
to
var header:URLRequestHeader = new URLRequestHeader ("Content-type", "text/plain");
That seemed to do the trick. As of today though this works in internet Explorer but no longer in Chrome, Safari, Firefox. I saw that Adobe put out an update/patch to flash and flash player yesterday - could that have anything to do with it?
If i remove the following:
var header:URLRequestHeader = new URLRequestHeader ("Content-type", "text/plain");
jpgURLRequest.requestHeaders.push(header);
Then the page successfully redirects but $GLOBALS['HTTP_RAW_POST_DATA'] is then empty so no image file can be created.
Is there an alternative header i can put that will solve this?
My code is:
AS3:
function createJPG(m:MovieClip, q:Number, fileName:String) {
var jpgSource:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight);
jpgSource.draw(stage);
var jpgScreenshot: BitmapData = new BitmapData(362, 310);
jpgScreenshot.copyPixels(jpgSource, new Rectangle(288, 89, 362, 310), new Point(0, 0));
var jpgEncoder:JPGEncoder = new JPGEncoder(q);
var jpgStream:ByteArray = jpgEncoder.encode(jpgScreenshot);
var header:URLRequestHeader = new URLRequestHeader ("Content-type", "text/plain");
var jpgURLRequest:URLRequest = new URLRequest ("http://www.url.com/save.php");
jpgURLRequest.requestHeaders.push(header);
jpgURLRequest.method = URLRequestMethod.POST;
jpgURLRequest.data = jpgStream;
var jpgURLLoader:URLLoader = new URLLoader();
navigateToURL(jpgURLRequest, "_self");
}
save.php
$imagefile=''.$imageURL.'';
$fp = fopen($imagefile, 'wb');
fwrite($fp, $GLOBALS['HTTP_RAW_POST_DATA']);
fclose($fp);
header('Location: https://www.url.com/your-image.php');
Managed to get this working now with the following code. Liam was correct on the Flash Player issue. Working now by separating saving the image and navigating to the url into 2 differents function:
function createJPG(m:MovieClip, q:Number, fileName:String) {
var jpgSource:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight);
jpgSource.draw(stage);
var jpgScreenshot: BitmapData = new BitmapData(362, 310);
jpgScreenshot.copyPixels(jpgSource, new Rectangle(288, 89, 362, 310), new Point(0, 0));
var jpgEncoder:JPGEncoder = new JPGEncoder(q);
var jpgStream:ByteArray = jpgEncoder.encode(jpgScreenshot);
var urlLoader:URLLoader = new URLLoader();
var header:URLRequestHeader = new URLRequestHeader("Content-type", "application/octet-stream");
var saveJPG:URLRequest = new URLRequest('http://www.url.com/save_image.php?name=filename';
saveJPG.requestHeaders.push(header);
saveJPG.method = URLRequestMethod.POST;
saveJPG.data = jpgStream;
urlLoader.addEventListener(Event.COMPLETE, goToCheckout);
urlLoader.load(saveJPG);
}
function goToCheckout(e:Event):void{
var url:String = 'http://www.url.com/show_image.php';
var request:URLRequest = new URLRequest(url);
try {
navigateToURL(request, '_self');
} catch (e:Error) {
trace("Error occurred!:");
}
}
save_image.php:
if(isset($GLOBALS["HTTP_RAW_POST_DATA"])){
$jpg = $GLOBALS["HTTP_RAW_POST_DATA"];
$path = "";
$id = $_GET["name"];
$file = $id;
file_put_contents($path.$file, $jpg);
echo "complete";
} else{
// error
}
Flash Player version 13.0.0.214 introduces some key security fixes. Unfortunately it also breaks navigateToUrl() by forbidding the changing of any headers in the request given to navigateToUrl(). This breaks POST requests that need to pass in security/session token headers, or to even change the Content-Type, e.g. to text/xml, etc. as you have done in your example.
Right now, as much as it sucks, our best known workaround is to downgrade clients to 13.0.0.206
Adobe proposes using external interface call as described here: https://forums.adobe.com/message/6396080
it uses JavaScript to replace the POST method
The solution proposed by odd_duck seems simpler though - would be great to see the php code as well.
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 );
};
i use LocalConnection between two swf in the same page.
What i'm trying to do is to use a String sent from one swf to the other, as variable into my URLRequest...
So i can trace "myVar" into the function chemin, but i didn't find how to use it into URLRequest
thank's for your help
swf that receive the var :
var lc:LocalConnection=new LocalConnection();
lc.client=this;
lc.connect("callBig");
function chemin(myVar:String){
trace(myVar)
}
var chargementXML:URLLoader = new URLLoader();
var fichier:URLRequest = new URLRequest(myVar);
Some references to help you out.
URLRequest.data: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/URLRequest.html?filter_flex=4.1&filter_flashplayer=10.2&filter_air=2.6#data
URLVariables: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/URLVariables.html
So, if you want to pass some variables in your request, you do the following:
Create new URLVariables() object
Assign it's reference to URLRequest.data field.
var fichier:URLRequest = new URLRequest();
var urlVars:URLVariables = new URLVariables();
urlVars["myVarName"] = myVar;
fichier.data = urlVars;
This way your server side app will recieve a variable myVarName with value of myVar.
var lc:LocalConnection=new LocalConnection();
lc.client=this;
lc.connect("callBig");
var receivedVar:String = "";
// somewhere else
function chemin(myVar:String){
receivedVar = myVar;
}
// later
var chargementXML:URLLoader = new URLLoader();
var vars:URLVariables = new URLVariables();
vars.myVar = receivedVar;
chargementXML.data = vars;
var fichier:URLRequest = new URLRequest(myVar);
private function getTitle(src:String):String{
var urlLoader:URLLoader = new URLLoader();
var rssURLRequest:URLRequest = new URLRequest(src);
var rss:XML = new XML;
var t:String = src;
urlLoader.addEventListener(Event.COMPLETE,
function(event:Event):void{
rss = XML(urlLoader.data);
t = rss.channel.title.toString();
});
return t;
}
I'm aware that this code doesn't work because the anonymous function doesn't work until after t is returned. How would I make it so that it works?
You won't be able to return the loaded data from this method. The reason for this is because the loading is asynchronous and doesn't not block the execution of subsequent code. Your best option is to move the vars out of the scope of the function and to write a second function to handle the COMPLETE event.
Something like the following should work:
var rss:XML;
var t:String;
var path:String = "some path";
var urlLoader:URLLoader = new URLLoader();
private function getTitle(src:String):String
{
urlLoader.load( new URLRequest( src ) );
urlLoader.addEventListener(Event.COMPLETE, onComplete );
}
private function onComplete(event:Event):void
{
rss = XML(urlLoader.data);
t = path + rss.channel.title.toString();
}
I realize that this doesn't really answer the question directly, though it is the best practice for handling data loading. If you really want to stop any code from executing before the data is loaded, it may be possible use a while loop after the addEventListener line to halt the player from until the data is loaded. This should probably be considered a not so elegant hack though.
private function getTitle(src:String):String
{
var urlLoader:URLLoader = new URLLoader();
var rssURLRequest:URLRequest = new URLRequest(src);
var rss:XML = new XML;
var t:String = src;
var complete:Boolean;
urlLoader.addEventListener(Event.COMPLETE,
function(event:Event):void
{
rss = XML(urlLoader.data);
t = rss.channel.title.toString();
complete = true;
});
while( !complete ) { /* sleep hack */ }
return t;
}
I haven't tested this, but it seems like it could work. The first example is recommended.