Writing a SWF to a Web Page from SQL Server
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.

December 11th, 2008 at 5:21 pm
[...] Vote Writing a SWF to a Web Page from SQL Server [...]