Blog Stats
  • Posts - 4
  • Articles - 0
  • Comments - 10
  • Trackbacks - 5

 

Monday, March 13, 2006

ActionScript to JavaScript: Let's get these two talking


One of the neatest features of Flash 8.0/ActionScript 2.0 is the improved facility for communicating with JavaScript. 

I'm an ASP.NET guy, and this is an easy way to send information from ActionScript to JavaScript to ASP.NET.  Hint: Use the HiddenField control.  This will enable you to create an application with zero postback(Until the last step, of course!).  Fast, fast, fast!  It is also possible to send data to a waiting web service or java servlet.

ActionScript uses the ExternalInterface.call method to reference a JavaScript function in the page containin the SWF. 

To call a JavaScript function from ActionScript: ExternalInterface.call(“myJavaScriptFunction“);

To send parameter(s) to the JavaScript function: ExternalInterface.call(“myJavaScriptFunction“, parameter1, parameter2);

The reverse is also true!  You can easily trigger ActionScript functions from JavaScript. 

ActionScript uses the ExternalInterface.addCallback method to make an AS function accessible to JavaScript. 

ExternalInteface.addCallback(“myJavaScriptFunction“, null, myJavaScriptFunction);

The first parameter is the name by which JavaScript will call the AS function.  The third parameter is the actual name of the ActionScript function.  For simplicity's sake, I use the actual function name.

Here's a complete example.  ActionScript calls a JavaScript function that captures the hosting server address (ex. geekswithblogs.net) and the complete path to the hosting page (ex. /mikeymac).  The JavaScript function then returns the value to a waiting ActionScript function. 

ActionScript:
//Calls JavaScript function returnLocationToActionScript on hosting page.
ExternalInterface.call("returnLocationToActionScri pt");
//Receives and processes location value from JavaScript function returnLocation
ExternalInterface.addCallback("getLocation", null, getLocation);
function getLocation(locationName:String):Void {
//Your specific code here. I added a trace as an example.
trace(locationName);
}

JavaScript:
function returnLocationToActionScript() {
//Get the server name(Ex: www.kirupa.com)
var domainname = new String(location.host);
//Get everything after server name(Ex. /forum/newreply.php)
var pathname = new String(location.pathname);
//Send domainname and pathname to ActionScript function "getLocation", where flashID equals the name or id of the embedded SWF.
flashID.getLocation(domainname + pathname);
};
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Friday, March 10, 2006

FTP and ASP.NET 2.0


I recently played with the new FTP capabilities of ASP.NET 2.0.  I created several directories on my web server, each of which harbored multiple files(text and images).  My goal was to create identically named directories on my FTP server, and then send the contents of the original web server directories to their new FTP server counterparts.  All things considered, this is a nice addition to the .NET quiver. 

Goes a little something like this:

1.) Creating  directories on an FTP server that match directories on your web server.

Dim path As String = Server.MapPath(".\directory1\")

Dim s As String

'******Iterate through the directories. 

For Each s In Directory.GetDirectories(path)

'******Some of my typically sloppy string wrangling

Dim b As Array = s.Split("\")

s = Convert.ToString(b(6))

'******Define name of target FTP server, and the name of the directory that will be created there.

Dim URI As String = "ftp://ftpservername/” + s

Dim ftp As FtpWebRequest = CType(FtpWebRequest.Create(URI), FtpWebRequest)

'******Provide login info.  Security is job one.  : )

ftp.Credentials = New System.Net.NetworkCredential("username", "password")

'******Specify whether the connection should be kept alive. Default value is true.

ftp.KeepAlive = False

'******Why are we here?  In this case, to create a directory on the target FTP server.

ftp.Method = WebRequestMethods.Ftp.MakeDirectory

Using response As System.Net.FtpWebResponse = CType(ftp.GetResponse, System.Net.FtpWebResponse)

response.Close()

End Using

Next

2.) Sending files from directories on your web server to identically named (See step 1) directories on an FTP server.

Dim d As String

'******Iterate through the directories. 

For Each d In Directory.GetDirectories(path)

'******Some of my typically sloppy string wrangling

Dim b As Array = d.Split("\")

d = Convert.ToString(b(6))

Dim f As String

'******Iterate through the files in the current directory

For Each f In Directory.GetFiles(path + d)

Dim c As Array = f.Split("\")

f = Convert.ToString(c(7))

'******Define name of target FTP server, the target FTP directory (as created in Step 1.), and the name of the file that will be sent there.

Dim URI As String = "ftp://ftpservername/ + d + “/“ + f“

'******Define the FtpWebRequest

Dim ftp As System.Net.FtpWebRequest = CType(FtpWebRequest.Create(URI), FtpWebRequest)

ftp.Credentials = New System.Net.NetworkCredential("username", “password")

ftp.KeepAlive = False

ftp.UseBinary = True

'******Why are we here?  In this case, to upload a file to the target FTP server.

ftp.Method = System.Net.WebRequestMethods.Ftp.UploadFile

'******Stream bytes from web server file

Dim fs As New FileStream(path + d + "/" + f, FileMode.Open)

Dim filecontents(fs.Length) As Byte

fs.Read(filecontents, 0, fs.Length)

fs.Close()

Dim requestStream As Stream = ftp.GetRequestStream()

requestStream.Write(filecontents, 0, filecontents.Length)

requestStream.Close()

Dim response As FtpWebResponse = ftp.GetResponse

response.Close()

Next

Next

 

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Hoff and Pepsi: Two Giants Unite As One


Best wallpaper ever. Period.

http://www.pepsisamba.com.au/hoff.html

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Friday, January 06, 2006

ASP.NET 2.0 Menu Control = skmMenu?????


I've been a satisfied user of Scott Mitchell's skmMenu control since the summer of '05. (visit http://www.skmmenu.com ) for details. Basically, skmMenu enabled an ASP.NET 1.X developer to create static or dynamic pull-down (pull-out if they're created vertically?) menus. Very nice.

While prepping for a conversion of my website to ASP.NET 2.0, I took some time to investigate the new Menu control. Imagine my surprise when I found it to be nearly identical to skmMenu. I converted my skmMenu to the new ASP.NET 2.0 menu control (took me about 2 minutes).

In situations like this, are the original authors of a control given compensation of some kind?
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
 

 

Copyright © Michael McMorrow