| Register
Saturday, May 17, 2008   

Dynamic Content - Flash and ASP

Created By  Chris Bizzell, at  8/6/2004 - 44 comments.

Click to view this author's website.

This article is going to look at utilizing ASP's FileSystem Object and Flash's new MovieClipLoader to build a dynamic photo gallery. Lots of times a client will say, "I have some images I would like to post on the web, can you create a photo gallery for me?" and after putting the application together, they come back and want to add images to the application. Instead of rebuilding the Flash document each time, a better solution can be achieved by building your list of images each time the photo gallery application is accessed. http://www.bizwerk.net/preload_jpegs/actionscript_com2.html

To start, create a folder called images under your web root. This will be the repository for our images. To update the application later, all you will need to do is place your new images in this folder.

Next we will use ASP to read the contents of this folder and list it's contents onto a web page. The ASP code looks like this:

As Flash uses URL encoded variables in the form of a name-value pairs, I use ASP to loop the the folder's content and append the image names to a variable called myList. Notice that each image name is separated by a comma, this will be used later when we parse out the individual image names inside flash.Make sure you set the path to the folder you set up earlier. The output of the script will look something like this:

images_list=c_jrob2_i.jpg,einstein.jpg,ftr1.jpg,img_girl_sitting.jpg,milscroll.jpg,uncut-ric-flair-1.jpg,

Now the Flash side of things.

Flash MX 2004 has a myriad of ways to load data into your application. I chose to employ loadVars() and the new MovieClipLoader Class for this example.

First we need to load the data from our ASP page to get the image names we will load into out photo gallery. For this I attach the following code to layer one frame one of our Flash movie:

cacheKill = new Date().getTime();
trace(cacheKill);
myVars = new LoadVars();
myVars.load("http://www.bizwerk.net/preload_jpegs/image_list.asp?"+cacheKill);
myVars.onLoad = function(){
trace(myVars.images_list);
image_array = myVars.images_list.split(",");
_root.image_array_length = image_array.length - 1;
trace (image_array.length);
loadPics(); 
}

A few things to point out here. At the top of the code a variable named cacheKill is created using the Date object. This variable is appended to the URL of our ASP page. As it will have a different value each time the user accesses the Flash application, Flash will go out and get the latest information, insuring that the user will always see the most current list of uploaded images. As was shown before, our ASP page returns a list of image names as so:

images_list=c_jrob2_i.jpg,einstein.jpg,ftr1.jpg,img_girl_sitting.jpg,milscroll.jpg,uncut-ric-flair-1.jpg,

For this to be useful to Flash, an array is created by splitting the variable images_list returned via ASP at the comma character as so: image_array = myVars.images_list.split(","); Next I assign a dynamic text variable with the value of our image_array length. This will be used later in a loop that displays out images. Finally a function called loadPics() is called that we will set up now to load the images into Flash via the attachMovie method.

Attaching our images to a movie clip. Setting up the thumbnail images Open the library and create a symbol - for simplicity create a rectangle and leave it at that. Right click on the symbol's name inside the Library window and give it a linkage ID of image.

Next, create a new layer named image loader and attach the following code to it's first frame:

myLoader = new MovieClipLoader();

We will use this object to load our images. The MovieClipLoader also has several callbacks that enable you to execute code at differing times during the loading of an external asset. These callbacks give the developer far greater flexiblity in that different methods can be executed based on the loading process. A complete list of callbacks can be found http://livedocs.macromedia.com/flash/mx2004/main_7_2/wwhelp/wwhimpl/js/html/wwhelp.htm?href=Part_ASLR.html:

We will use the onLoadComplete callback a bit later in this example.

Actionscript for out loadPics function.

function loadPics(){
myY = 80;
myX = 40;
for (i=0; i<= _root.image_array_length - 1 ; i++){
this.attachMovie("image","image" + i, i); //attach the movie 
this["image"+i]._y = myY; 
myLoader.loadClip( "http://www.mysite.com/images/" + image_array[i]+"?cb=11" , this["image" + i]);//load images into clip
if ((i+1) % 2 == 0){
myY +=100;
trace(myY);
this["image"+i]._x = 40;
}else{
this["image"+i]._x = 140;
}
}
}

Here is the key line in this code:

myLoader.loadClip( "http://www.mysite.com/images/" + image_array[i]+"?cb=11" , this["image" + i]);//load images into clip

I won't go into attachMovie here but notice that we utilize the loadClip method of our myLoader object to load the items in our image_array into the attached instances of our image clip. As arrays in Flash start with 0, we set the initial value of i to 0 and loop through until we get to the length of our array.

To format the placement of our attached clips the following lines:

if ((i+1) % 2 == 0){
myY +=100;
trace(myY);
this["image"+i]._x = 40;
}else{
this["image"+i]._x = 140;

This code checks to see if the value of i is divisible by two using the modulo operator %. If the value of i is an odd number a new line is created for our clips.

Enhancing the Interface

Open the components window and place an instance of the loader component and the progress bar component on the stage. Give the loader component an instance name of cont and the progress bar an instance name of pBar. We will load a larger copy of each picture into the loader component whenever it's thumbnail image is clicked. Set the mode parameter in the properties window to event and the source to cont - the instance name of our loader component.

Lastly we need to assign the actions to our attached movie clips that will load the larger image into our loader component . The MovieClipLoader enables us to assign code to dynamically created clips once the image has fully loaded into our application using the previously mentioned onLoadComplete event to trigger the following code:

myLoader.onLoadComplete = function(targetMC){ 
targetMC.onPress = function() { 
photoName = targetMC._name;
trace("pname="+photoName);
// our dynamic clips are named image1 - image 2 and so on. We need to chop off the
// image prefix from our name utilizing the slice method as shown in the next line of code
photoName = photoName.slice(5);
// photoName now has a value of 1,2,3 etc 
cont.autoLoad = false;
cont.contentPath = "http://www.bizwerk.net/preload_jpegs/images/" + image_array[photoName];
// set up the progress bar to monitor assets being loaded into our loader component
pBar.source = loader;
cont.load();
}
}

By assigning the onPress actions after the clip is fully loaded, we insure that each attached clip will be given the appropriate actions.

Check out the code comments for additional information regarding this code block.

Test your movie and see your thumbnails load. Click on a thumbnail and watch as the progress bar monitors the download of the larger image into the loader component.

That's it. I hope that this photo gallery example enables you to move forward in your understanding of the power of Flash when teamed with dynamic data sources.

Need Professional Help For Your ActionScript Project?
ActionScript.com Consulting Services provide top quality professional ActionScript consulting to businesses around the globe. If you have a professional project in need to world-class talent, tell us about your project by requesting a quote today.

Reader Comments

  1. Heino  Replied:
    ( 8/12/2004 At 6:56 PM)

    I tried this example for over 3 times and everything seams to work fine.

  2. Heino  Replied:
    ( 8/12/2004 At 7:09 PM)

    Perhaps it's usefull to link a zip file with an exampleproject to this article!

  3. Chris Bizzell  Replied:
    ( 8/14/2004 At 3:01 AM)

    What are your trace statements saying to you? make sure that you are loading data.
    LINK TO FLA:
    http://www.bizwerk.net/preload_jpegs/actionscript_com2.zip

  4. ben  Replied:
    ( 8/20/2004 At 6:05 AM)

    I have the same problem too. My images won't load up.

  5. cvb  Replied:
    ( 8/24/2004 At 6:51 PM)

    Instead of paging you can load all of the images into a single clip and then scroll that clip. If you do this you will need to make sure you gauge the width if the clip each time an image is loaded into it and use that value to set the x and y coordinates of your next iamge to be loaded.

  6. cvb  Replied:
    ( 8/25/2004 At 4:58 PM)

    You have an array of the images, simply modify this line:

  7. Jason M. Batchelor  Replied:
    ( 8/25/2004 At 5:55 PM)

    Some great ideas, but there are a few things you need to warn the users about. First off is the ASP script. You have created two server objects but from what I see of the script above, you haven't killed them off at the end of the script. You always should destroy objects you create in ASP once you're done with them; otherwise, you're leaving your pages open for memory leaks, which lead to dying servers, which leads to unhappy users. A cleaner way of dealing with this might be:

  8. S.C.  Replied:
    ( 8/27/2004 At 9:16 AM)

    Isn't MovieClipLoader available only with Flash player 7? It might be nice to use, but wouldn't anyone who uses an older player version would get back blank pages?

  9. cvb  Replied:
    ( 8/27/2004 At 4:10 PM)

    Flash 7 now has an adpotion rate of over 70%. You can use other methods, bit the MovieClipLoader affords you a lot more power via the callbacks.

  10. Jay BRacken  Replied:
    ( 9/3/2004 At 5:50 AM)

    Just wondered how i could load plain buttons(w/mouseovers etc.) instead of thumbnails as my buttons.

  11. Trevor Adams  Replied:
    ( 9/3/2004 At 7:10 AM)

    Also another item you might want to do is cache the Asp page, so that when the user(refreshed for any reason) you don't make too many unwanted calls to the server, It will save memory for more needed items on your site and others.

  12. socrate  Replied:
    ( 9/7/2004 At 2:03 PM)

    This tutorial seems great if I can get it to work but however I can’t seem to prevent Flash from crashing when I try it. My “myY += 100” trace goes up to est. 459,000, I think that can be wrong. Is there anyone that can post a file of this tutorial because I think I got some things mixed up in Flash. Thanks.

  13. socrate  Replied:
    ( 9/7/2004 At 2:20 PM)

    O yes I forgot, if someone has the files can you please them to my e-mail that would be great! Thanks soooo much.

  14. socrate  Replied:
    ( 9/8/2004 At 10:19 AM)

    If you are dragging and dropping or right clicking then copying and pasting the button(which is actual a movieclip in this case) onto the stage you are not changing the “Instance of” which you can find in the Properties Panel. This may be the problem. By changing the script of a copied object that has the same “Instance of” as the original, you are changing both scripts of the objects simultaneously. If this is the case, you can solve this quickly by right clicking on the movieclip in the library and select “Duplicate” which will change the “Instance of.”

  15. Adam  Replied:
    ( 10/10/2004 At 10:13 AM)

    Alright so I got it to work anyone have ideas on how to deal with large number of images? I was thinking one of two options:
    Load all the images into a scroll panel thus allowing the user to scroll the images and click to enlarge.
    Add to the actionscript to have the array only build out the first 6 or so images are displayed at a time with a next and previous buttons that will call certain numbers from the array for display.
    Any comments on which might be the better option....
    Thanks

  16. cvb  Replied:
    ( 10/12/2004 At 6:33 PM)

    Look at my new article that uses the datagrid. It may be a better a better fit for you.

  17. paulhiles  Replied:
    ( 10/26/2004 At 3:36 PM)

    This looked very promising, I've been looking for something that combined a Flash image viewer with server-side scripting. The demo link worked perfectly in IE, however, on Firefox the images don't load correctly.

  18. Taltos  Replied:
    ( 11/9/2004 At 12:57 AM)

    Does anybody have a .fla file they can send me so I can analyze this in a more hands on fashion? I am definately new to scripting, so context means EVERYTHING to me. I tried starting this up, but got hung up at the very beginning.

  19. Brian  Replied:
    ( 11/29/2004 At 4:06 PM)

    I got the gallery working and its GREAT!!! I even got it to work with a .asp thumbnailer so it dynamically creates thumbnails of large images, anyway I need a couple of last help things on it.
    1 - how can I make the images scroll or attach some kind of scroll bar to it since I would like to use something like 20 images.
    2 - can I set the images to 3 or 4 columns.
    thanks it would be great if someone helped me with this.

  20. cvb  Replied:
    ( 11/30/2004 At 11:24 PM)

    Here is a link to the fla

  21. gary  Replied:
    ( 12/2/2004 At 2:38 AM)

    Hi, can anyone send me a .fla file of this example?
    I am having some difficulties in the Flash side of things. I would appreciate any help anyone could give me. Thanks.

  22. Guy Leonard  Replied:
    ( 12/9/2004 At 4:24 AM)

    I have been having trouble using your code and I was wondering if you could send me the URL linking to the FLA file so I could better understand how it all works.
    Thanks,
    Guy

  23. Anoop  Replied:
    ( 1/25/2005 At 8:55 PM)

    Hi,
    I am trying to load data from a Access file using ASP.
    1. The data takes about 8 seconds to load.
    2. The loaded data has junk characters in it (eg: ' "SINGLE QUOTE", is shown as ‘)
    Waiting for some one to help me out......
    Thankyou
    Anoop

  24. cvb  Replied:
    ( 1/26/2005 At 6:06 PM)

    http://www.bizwerk.net/preload_jpegs/actionscript_com2.zip
    Above is the link to the fla - good luck

  25. Amit  Replied:
    ( 3/16/2005 At 6:56 PM)

    HI,
    I'm using an flash player to play mp3 file.My ASP Page Dynamically creates an xml file using database And flash player takes the path of song from this XML file.The problem with this is the flash player page requires an refresh to play song and not start at loading.I used all page refesh like response.redirect,wondow.reload,meta tag but nothing works.Please anybody help me!

    Thank You
    Amit

  26. Abel Knezevic  Replied:
    ( 3/24/2005 At 12:36 PM)

    Simply amazing! I will place a link to this page on my forum for my students!

  27. alireza  Replied:
    ( 4/16/2005 At 9:51 PM)

    Hi
    it was great . I use it succesfully but I can`t scroll the images which are more than 20 pictures(for instance).
    please help me ^_^
    sos

  28. Anoop  Replied:
    ( 4/20/2005 At 2:22 PM)

    Hi,
    This is regarding the data that is loaded from the access - asp to flash. The problem is that the data if contains any "&"(ampersand) it distroys the data line up that is pushed in the flash. Is there any solution so that I can call this data easily....
    Thanks
    Anoop

  29. B.Mohamed Arif  Replied:
    ( 4/20/2005 At 8:23 PM)

    Dear Mr. Chris Bizzell, i have seen your article in actionscript.com and it is 100% usefull for everyone who like to use the image gallery.
    also, i need a small example of data, which i have to receive from asp to flash(mx) thing is i need to strore all variables from asp to flash as array. i tried several types of process, but i can't get it. could you please help me?
    regards,
    Mohamed Arif

  30. Ajay Saharan  Replied:
    ( 4/29/2005 At 10:02 AM)

    Hi All,
    I want to play any clip through the asp code with windows media player. I want to manage the time like how much video completed, and when i come back on that page i want to start from the time when i stop it last time. I want to get the time of stop and store it in the database and when i start it again i want to get that time from database and start from that time. If any body have idiea please give code or idea.
    I don't want to show the images i want to show the movie clip.
    Thanks
    Ajay Saharan

  31. jitendra  Replied:
    ( 5/12/2005 At 5:12 PM)

    Hi 2 all, I need a small example of data, which i have to receive from asp to flash(mx) plz help me and send me solution as soon as possible.

  32. Eric  Replied:
    ( 5/16/2005 At 3:27 PM)

    How could this work if the images are stored in a database as binary files - and saving them temporarily is not an option?
    Thanks

  33. Chua  Replied:
    ( 5/19/2005 At 5:54 AM)

    Hi, may I know whether this image gallery will work for GIF or PNG pictures? I am looking for loader in flash that can load GIF and PNG pictures, can anyone please kindly give me a hand? Thanks..

  34. Arun  Replied:
    ( 5/31/2005 At 8:11 AM)

    Can anyone plz mail me the .fla src...

  35. David  Replied:
    ( 7/9/2005 At 9:30 AM)

    I am new to Flash and using HTML on my website, which is mostly a collection of family photos. I now have Flash MX and have combined 4 photos into a test .SWF file that came with Flash for 'flipping' through the photos. I am trying to get away from taking up so much load time on photographs by using the flash photoflipper...can I use this to make them 'load-on-demand' only, so that when a person flips the 'next' button, that only then does it load an image???

  36. Robert  Replied:
    ( 9/21/2005 At 7:34 PM)

    I have problems with the asp file
    it has an error compilation or something like that
    error: object server asp 0177 (0x800401f3)

  37. Vishal  Replied:
    ( 9/23/2005 At 10:11 PM)

    This helped a lot. Thank you very much!
    Would you know how I can get started for creating a slideshow of images?

  38. Naresh  Replied:
    ( 9/26/2005 At 3:24 PM)

    How i can load images & text in flash with the help of ASP code??
    pls tell me with an example it will easy to understand
    thanks

  39. Vishal  Replied:
    ( 9/28/2005 At 11:26 PM)

    Hello Naresh,
    The article explains in great detail how to populate a Flash presentation using ASP. If you read through the previous comments, you will find a link to the source FLA file which you can open and see to understand.
    Essentially, a seperate ASP file is created specially for "Response.Write"ing the information on the screen that Flash will use. Flash then "executes" the script and the written information is interpreted as variables by Flash, provided that the output is in the correct format. You can use Flash ActionScript to manipulate the obtained data.
    Thanks, Vishal

  40. Jun  Replied:
    ( 11/3/2005 At 4:53 AM)

    Hi Is there a way I can create a dynamic content using FLASH and PHP? I'm still a newbie. I hop somebody could hlp me. Thanks

  41. Rabi  Replied:
    ( 11/8/2005 At 8:27 AM)

    This is cool example. Helped me a lot. To go further, I would like to work more with xml and asp integrating it with flash. If anyone has worked with xml, asp and flash please suggest me the link. I was thinking of generating xml from flash and let asp save it to a file into the server so that the same flash can reload it after its updated.
    This example was really good. Thank you.

  42. divyesh modi  Replied:
    ( 1/12/2006 At 7:31 AM)

    hi.
    i want to conact my flash form with asp
    please give me the example for this mattar
    basically i am trying to conact my flash made
    "loginid" & "password" input textbox with asp.
    send me mail as posibal as eraly.
    tanking you !

  43. chris frank  Replied:
    ( 2/2/2006 At 7:33 AM)

    Hi, can anyone send me a .fla file that can be used for making images scroll up and down - a scroll bar ?
    Thanks

  44. Adam Benson  Replied:
    ( 6/6/2007 At 5:44 PM)

    Ok.... I'm about to give up. I have tried this over and over again in many different ways, and my asp loaded variables ALWAYS come up "undefined". Thus nothing past the first few steps works. I have even mimicked you asp file and code EXACTLY! It works fine when I have your page as the DB connection, but returns "undefined" when I switch the page to mine. my variables are copied and pasted from your example...."undefined". I've tried it with .txt files... "undefined" What am I doing wrong?
    Thanks for your help.
    Adam

Login to post your comments. If you do not have an account with us please Register.
Copyright 2005 by ActionScript, Inc.   |  Privacy Statement  |  Terms Of Use  |  ActionScript Client Extranet