After a lot of research and some help from my friend Mark, I finally put together a small demo of posting data from a client side flash file to a server. In this example the flash file simply sends the text “Hello” to the server, and the sever reads this and saves it to a text file. Simple stuff but unusually hard to figure out, with a serious shortage of helpful information online.
Uses
- Your user plays a flash game you made, and you want to submit their high score back to the server to store in a databse
- Your flash file uses a form to collect data from the user which needs storing in a database
ACTIONSCRIPT
var store:LoadVars = new LoadVars(); //Make a new variable storage collectionstore["testField"]=”Hello”; //Create an entry in the variable collection named “testField”
store.sendAndLoad(“http://localhost:51527/testForm/Default.aspx”,store,”POST”); //Send variables to ASP.NET
NB: – The sendAndLoad method posts the data silently without opening a browser window,hence the aspx page is never rendered to the user.
NB:- The second argument of sendAndLoad can be any LoadVars object used to retrieve a response from server if one is present (not rnecessary for one way data posting so here I used the original LoadVars object) .
ASP.NET (Server Side)
string field = Request.Params.Get(“testField”).ToString(); // get the parameter StreamWriter me = new StreamWriter(“C:/dump.txt”,false);
me.Write(field); //Write the parameter to a file
me.Close();
Heya, just looking at your code here and im trying to do the same thing although i am getting a error on up:
The type or namespace name ‘StreamWriter’ could not be found (are you missing a using directive or an assembly reference?)
are you bringing in any specific assembelies, /includes, or any Using statments?
Joe
Hi, my appologies, in order to use the StreamReader / Writer objects you muse include the Input/Output library…..
using System.IO
Hope that helps
Goodday, I tested your code speed compared with webservices and it works fantasticly. Flash player 8 > have great problems with security policies and so this solution really helped out with my exam!
Respect! *add kudos*
Keep it up, awM,
Hi all,
under which event should write asp.net code above?
should it be form load event or another event?
Thanks in advance
H
Hi haitham,
I would write the code above under the page load event. This is because an asp.net event is not being fired other than requesting the page.
Dean
Hey Tom,
What version of actionscript are you using here? Or does it work in both 2 and 3?
Thanks,
Dean
Hi,
Could u please post the example source code for us.
At least if you please send it 2 my email address i’ll be thankful.
I’m looking forward for your help.
Thank you very much
Hooman
rainman_t@yahoo.com
In AS3, Example from ADOBE:
package {
import flash.display.Sprite;
import flash.events.*;
import flash.net.*;
public class URLRequestExample extends Sprite {
public function URLRequestExample() {
var loader:URLLoader = new URLLoader();
configureListeners(loader);
var request:URLRequest = new URLRequest(“XMLFile.xml”);
try {
loader.load(request);
} catch (error:Error) {
trace(“Unable to load requested document.”);
}
}
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);
trace(“completeHandler: ” + loader.data);
}
private function openHandler(event:Event):void {
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);
}
private function httpStatusHandler(event:HTTPStatusEvent):void {
trace(“httpStatusHandler: ” + event);
}
private function ioErrorHandler(event:IOErrorEvent):void {
trace(“ioErrorHandler: ” + event);
}
}
}
For URL Variables:
var variables:URLVariables = new URLVariables();
variables.exampleSessionId = new Date().getTime();
variables.exampleUserLabel = “guest”;
request.data = variables;
Instead of GETURL as3 uses navigateToURL:
var url:String = “http://www.adobe.com”;
var variables:URLVariables = new URLVariables();
variables.exampleSessionId = new Date().getTime();
variables.exampleUserLabel = “Your Name”;
var request:URLRequest = new URLRequest(url);
request.data = variables;
try {
navigateToURL(request);
}
catch (e:Error) {
// handle error here
}
hi im not sure how to do this..could you send a source code with the flash? AS2 thanks alot!
hey, this is exactly what i need to do, could you share the coda with tha .fla example please?
Thanks!
Hi andrew,
Unfortunately as this post is so old I cannot find any source code. Hopefully the article should explain how to reproduce the source yourself. If you still have any issues, drop me an email.