Blog

Posts Tagged ‘.NET’

Porting String.Format to ActionScript

Tuesday, July 7th, 2009

One of my favorite methods in the .NET framework is the string class’s Format method. If you are unfamiliar, you can read more about it here: http://msdn.microsoft.com/en-us/library/aa331875.aspx. The .NET implementation contains numerous formatting options; however, I am only interested in the basic idea of replacing format items in a string with actual values passed to the function. This reduces the need for complex concatenations, which makes code more readable.

The ActionScript version I’ve implemented works in much the same way as its .NET counterpart. The first argument is a format string containing format items. For example, to display the current date, we may set up the following format string, “Today is {0}.” Then, we pass an array of objects to use as replacements for the format items. In our example, we would pass a new array literal with one item, [ new Date().toDateString() ]. So our entire call would look like the following:

// display today's date in the text field txtDate
txtDate.Text = StringUtils.Format( "Today is {0}." , [ new  Date().toDateString() ] );

The function will return a new string that has the {0} format item replaced with the evaluated value from the Date object. It is important to note that ALL instances of {0} will be replaced, not just the first occurrence. So the function itself is very simple, but can dramatically improve the readability of code.

I have provided the source below and a link to download StringUtils.as, which has the necessary Replace function as well. The only note I would make about the code is the use of toString() on each object in the array passed to the function. This allows calling code to pass arrays containing any type of object and omit calling toString() on each instance (provided the toString() implementation is acceptable). Hopefully you find this as useful as I have!

Source:

public static function Format( formatString:String , args:Array ) : String
{
   var result:String = formatString;
   for (var i:int = 0; i < args.length; i++)
   {
      result = Replace(result, "{" + i + "}" , args[i].toString() );
   }
   return result;
}

Download Source Files

Writing a SWF to a Web Page from SQL Server

Thursday, December 11th, 2008

We recently had a requirement to store SWF files directly in our database as a file bytes field. Our client had a need to dynamically change multiple SWF files via an administrative site. Due to the clustered server environment, storing the files in a database and pulling them out on a new request was the best option.

Uploading the file was fairly routine, but actually getting the file out of the database and embedding it in the page proved to be interesting. Not only did we need a way to create this file on the fly, but we also needed it in such a way that the browser would be able to render it as Flash content.

We created an APSX page that required a parameter “id” of the SWF we needed from the database. The files bytes are read from the database as any typical file, execute a sql server stored procedure that returns an Image Data Type containing our file bytes. Then we set the ContentType of the Response to the Multipurpose Internet Mail Extensions (MIME) type “application/x-shockwave-flash”. Finally we write the file bytes to the response stream. IIS will then send the MIME along with the file to the browser.

We initially tried to use the URL of our ASPX page directly in the object and embed tags. Instead of listing our SWF as “filename.swf”, we attempted something more along the lines of “getSwf.aspx?id=1″. A noble idea, but it lead to little more than a blank page. Our next approach was similar, but we used SWFObject to write out the Flash content instead. Again, the URL we passed to SWFObject was an ASPX page that was returning the file bytes of the SWF. Still no luck, just a blank page.

It started to look as if this might not be possible, but then we had a different idea. What if we gave Flash player a shot at rendering the file bytes? We would write our own static SWF that did nothing more than attempt to load a URL it received as a parameter. In this way, we could provide a hardcoded URL to SWFObject and rely on the Player to handle the dynamic piece of the puzzle.

In short, it worked! We wrote a quick SWF (targeted to Flash Player 7) that simply used a MovieClipLoader to load a URL into a container clip. The URL itself was an ASPX page that returned the file bytes of a SWF as stored in the database! Flash player understood the response and was able to load and render the SWF just as if we passed a direct filename with a “.swf” extension.