Introduction to the communication principle of Flash and ASP

Source: Internet
Author: User
Tags format iis variables reference client advantage

Often someone asked me the flash message board production methods, but this thing a sentence can not be said clearly, so the germination of the idea of writing a tutorial. But then another thought, teach people to fish, not better to teach people to fishing, or better concentrate on the flash and ASP communication principle, the principle of the specific project can be free to play.

This tutorial is for beginners, and before I begin the tutorial, I assume you have the basics of flash operations, understanding the IIS configuration and the operating environment. Although there are many ways for flash to communicate with ASP, in this tutorial I choose to use the Loadvars class, because the Loadvars class is easy to master, easy to explain, and two because it does not involve too many other aspects of knowledge. In addition, I used in the ASP is a JS script, because the JS script and as very much like, as long as you have a certain as the basis, JS can not learn to understand the basic. Finally, flash version, I choose Flash Pro 8.0 Simplified Chinese version, SWF released as as2.0,flashplayer8.0.

Before reading my tutorial, it is recommended that you take a few minutes to read the "Flash Basics Development Habits " to help you understand my code.

Directory :

    • Loadvars Class Basic explanation (Loadvars category hereinafter referred to as LV)
    • Basic input and output in ASP
    • The principle of LV and ASP communication
    • Getting Started with ASP operations database
    • The comprehensive application of LV and ASP
    • On the principle of other communication methods

Here I will mainly focus on my tutorials, to distinguish some of the overall indoctrination of the tutorial, I would have a real focus on this class of two methods and an event: Load, Loadandsend method, and onload events. If you need a more detailed understanding of the LV class, suggest you check help: Help →flash help → all books →actionscript 2.0 Language Reference →actionscript class →loadvars. Of course you can also search loadvars directly.

LV and TXT newsletter .
Not to talk about ASP? How do you speak of txt? Oh, do not worry, in fact, the LV and ASP communication principle and the principle of txt communication is the same. TXT you often use, speaking up more easily understand.
LV and TXT communication need to use the "variable"/"value" pairing data mode in TXT. For example: wenben= I want to do flash message book. Here "Wenben" is a variable, "I want to do flash message book" is the value, and "=" is their pairing way, that is, the way to establish contact. OK, now we build a "lv_shiyan.txt" text file and enter in it: neirong_txt= I want to make a flash message book. Then in the same folder to create a "Lv_shiyan.fla", in the first write code:

Set the encoding, or it will display garbled
System.usecodepage = true;
Instantiate a LV Object
var shiyan_lv = new Loadvars ();
Load external text
Shiyan_lv.load ("Lv_shiyan.txt");
After successful loading
Shiyan_lv.onload = function (Chenggong) {
if (Chenggong) {
Get External text content
var neirong = Shiyan_lv.neirong_txt;
Output text content
Trace ("The textual content that you load is:" +neirong);
}else{
Load failure give prompt
Trace ("Load failed");
}
};

Run the above code, you will see in the output panel "You loaded text content is: I want to do flash message book", this shows that the external "lv_shiyan.txt" in the content has been successfully loaded. Interested friends can change the txt filename, test the failure of loading.

through the above code, we learned the following knowledge :
1, how to instantiate a LV object (using the New keyword)
2. How to load an external text file with the LV object (using the Load method)
3, how to determine whether the external text file is loaded successfully (using the OnLoad event)
4, how to obtain and utilize the contents of an external text file after it has been successfully loaded.
In an external text file, we use the "variable/value" pairing data format, in order to be used by the LV object, when the external text file loaded successfully, the variables will be recorded by the LV object, and in as as the LV object reference external variables in the way: Lv object. The name of the variable in the external text file. The above code is: Shiyan_lv.neirong_txt.

The above example tells the basic principles of LV and TXT communication, and now let's expand, what if we want to use multiple variables in an external txt? The answer is actually very simple, we only need to use multiple "variable/value" pairs in txt, and separate each "variable/value" pair with "&". We still use the "lv_shiyan.txt" file of the above example, this time to change the contents of the content into the following form:
Neirong1_txt= beat Loadvar Class! &neirong2_txt= victory over asp! &neirong3_txt= I can make flash message board!
Then replace the code in "Lv_shiyan.fla" with the following:

System.usecodepage = true;
var shiyan_lv = new Loadvars ();
Shiyan_lv.load ("Lv_shiyan.txt");
Shiyan_lv.onload = function (Chenggong) {
if (Chenggong) {
Get External text content
var neirong1 = Shiyan_lv.neirong1_txt;
var neirong2 = Shiyan_lv.neirong2_txt;
var neirong3 = Shiyan_lv.neirong3_txt;
Output text content
Trace ("The textual content that you load is:" +neirong1+ "/" +neirong2+ "/" +neirong3 ");
}else{
Trace ("Load failed");
}
};

Run the above code, in the Output window we can see "Beat Loadvar Class!" /victory over asp! /I can make flash message board! , which shows that we have all the variable contents in the external txt.

→ Interested friends can change the contents of "Lv_shiyan.txt" into the following form to see the output has not changed:

&neirong1_txt= beat Loadvar Class!
&neirong2_txt= victory over asp!
&neirong3_txt= I can make flash message board!
And then we'll try it again. Output results:)
&neirong1_txt= beat Loadvar Class! &
&neirong2_txt= victory over asp! &
&neirong3_txt= I can make flash message board! &
The reason is actually very simple, oneself think about pull:

→ Shout, OK, LV class first here, the following is the basic format and syntax of ASP, especially input and output statements.

ASP after a long period of accumulation, its content is very rich, but if only to develop a simple message book, that only need to master the most basic input and output can be.

We create a new asp_jichu.asp file, enter "basic input and output in ★asp" in it, and save it. OK, so we'll create an ASP file. What do you think? Very simple: Then we run this ASP file under IIS, and we see "basic input and output in ★asp" on the Web page. Although the content can be displayed, but such a display has little effect on us. Why is ASP? This is mainly because it completes the server's interaction with the client, such as it can receive the variables sent by the client, and displays the information the customer needs in a specific way based on these variables. And the above "asp_jichu.asp", and did not play any interaction, also lost the meaning of ASP.

Before beginning the interactive explanation formally, it is necessary to talk about the composition of ASP file first. The composition of ASP file can be summed up in one sentence: All ASP statements begin with "<%" and End With "%>". One more thing to note is that in IIS, the general default ASP uses VBScript, but we use JavaScript, so at the beginning of the ASP file we need to add a sentence:

<% @LANGUAGE = "JAVASCRIPT"%>

To get to the point, first talk about passing variables to the ASP. There are two kinds of methods, the common get and post methods. The Get method is suitable for passing a small amount of content, generally within 2K, variables and content will be appended to the URL, the variable to "?" At the beginning, the variable/value pairing principle is also followed in the previous article in terms of variables and content. The Post method can deliver a large amount of content and the content will not appear on the Web site. Although the get-way delivery is less and less secure, it is very intuitive to do a tutorial demo. Now I'm going to demonstrate how ASPs receive variables in this way. Or take advantage of the "asp_jichu.asp" file we started building, enter the following and save:

Basic input and output <br> in ★asp
<% @LANGUAGE = "JAVASCRIPT"%>
<%
var Neirong;
Get the contents of a variable in the URL
Neirong=request ("Neirong_wangzhi");
Show variable Content
Response.Write ("The Address bar passes over the content is:" +neirong);
%>

Then run the file under IIS, and we'll see the page display:

Basic input and output in ★asp
The address bar passes over the content: undefined

First I need to "asp_jichu.asp" in the statement to explain, in the ASP, to receive the data is the "Request" object, and to output the content is the "Response" Object "Write" method. It should be noted that when the request receives the variable, it needs to be enclosed in quotation marks, and response is not required when the variable is output. In this case, the above code is not difficult to understand, at first we define a variable "Neirong", and then use it to record the contents of the variable "Neirong_wangzhi" passed to the ASP, and finally output "Neirong". But we did not assign value to "Neirong_wangzhi", so we got the "undefined". Now we use the Get method to assign the variable "Neirong_wangzhi" to see how it works.

As we have already said, the variables and contents of the Get method will be displayed in the browser URL, but this display is an intermediate process, the ASP is based on this intermediate process, that is, the information in the address bar to receive variables and processing the final output content. So we can take advantage of this intermediate process, directly in the browser address bar for variable assignment. Open the Running "asp_jichu.asp", and at the end of the URL with the following content, press ENTER to see how the page changes?

? neirong_wangzhi= I'm going to make my own Flash message board

Haha, did you see it? The page displays:

Basic input and output in ★asp
The content that the browser passes over is: I want to make the Flash message board by myself

What does that mean? We received the variable content passed in the address bar, we succeeded:
Suspected!? What are you staring at? Why don't you cheer with me? Is it the way you get passed or don't you understand? Oh, never mind, through the above demo, you just have to remember the following knowledge points on the line:
The composition of the 1,asp file (all statements are written between "<%" and "%>")
2,asp How to receive externally passed variables (using response objects)
3,asp How to display content in a browser (using the Write method of response)
What, my request is not high? Just remember the above three points, you should be fully confident to understand the content of my below, come on!

Friendly tips :
Do we change the contents of the "asp_jichu.asp" file into the following form to see whether the content displayed in the Web page will be changed?

Basic input and output <br> in ★asp
<% @LANGUAGE = "JAVASCRIPT"%>
<%
var Neirong;
Neirong=request ("Neirong_wangzhi");
%>
What the browser passes over is: <%response.write (neirong)%>

Change the following form to see if there are any changes?

Basic input and output <br> in ★asp
<% @LANGUAGE = "JAVASCRIPT"%>
<%
var Neirong;
Neirong=request ("Neirong_wangzhi");
%>
What the browser passes over is:<%=neirong%>

We will find that the results of the above three forms are the same, we can see that the ASP's writing is very flexible, we have to grasp its essence, not to be confused by various surface forms: This is the last line of code to be explained. <%=neirong%> is a short form of output that acts as a Response.Write and only applies when an ASP statement has only one row and only a simple variable is exported.

Sound, the knowledge of ASP temporarily know so much on the OK, the following exciting moment came, we want to start the interaction between ASP and Flash!

After the first two sections of learning, we have been familiar with the use of the LV class, as well as ASP's basic format and input and output. Now let's see how these two things blend together. Remember the example of the txt I gave when I was talking about the basics of the LV class? LV and TXT communication, TXT data need to write "variable/value" matching format, in fact, it must be written in this format is not determined by TXT, it is determined by the characteristics of LV, that is, LV and text file communication needs "variable/value" this data format. ASP file is actually a text file, ASP and LV communication, it output content format must follow the "variable/value" matching rules.

All right, let us from the code to an intuitive experience, we also use the "★LV Class Foundation" that section of the passing multiple variables of the example, remember that TXT file name: "Lv_shiyan.txt", OK, now we directly to the "Lv_shiyan.txt" Change to "Lv_shiyan.asp" also replaces Lv_shiyan.txt in the sentence shiyan_lv.load ("Lv_shiyan.txt") in the "Lv_shiyan.fla" code. Shiyan.asp, and then directly in the flash editing environment according to Ctrl+enter Test film, we found Output window output: "Beat Loadvar Class!" /victory over asp! /I can make flash message board! ", just like before the change.
Faint! That's OK!? What the hell is this? is the flash and ASP communication? How do you not need IIS all right? is that flash and txt communication? But the name of the suffix is clearly ". asp"!? In fact, the volcano here is not very clear, but I prefer to understand it as a flash and ASP communication, just because this ASP file does not have ASP statements, do not need IIS support to display content. If you have to ask for a statement, the most conservative answer is: Flash and text file communication:

Hehe: Well, don't be fooled. The above section is not clear about it, in fact, I transition from txt to ASP's purpose or more intuitive to tell you: Flash display ASP content and TXT is consistent. As long as we find a way to the ASP output data format into a "variable/value" pairing on the line!

In the TXT and around such a long time, we worry about it, OK, now I want to play the real, ASP officially debut:
First we change the contents of "lv_shiyan.asp" into the following form:

★LV and ASP Communication principle <br>
<% @LANGUAGE = "JAVASCRIPT"%>
<%
Response.Write ("&neirong1_txt= Beat Loadvar Class!") &neirong2_txt= victory over asp! &neirong3_txt= I can make flash message board! & ");
%>

Let's run it under IIS, and the page will display the following:

★LV and ASP Communication principle
&neirong1_txt= beat Loadvar Class! &neirong2_txt= victory over asp! &neirong3_txt= I can make flash message board! &

Well!? How so familiar ah, congratulations you, correct, this is not exactly "Lv_shiyan.txt" in the content! Just "★" after the title replaced by this section of the title: Well, then, smart you, now have you expected to what I want to do next? Congratulations, you're right again, why are you so smart, the next step of course is to display these data in Flash. Say dry, go back to "Lv_shiyan.fla" in the editing environment, press Ctrl+enter again to test the movie. Haha, success! Output window shows: "Beat the Loadvar Class!" /victory over asp! /I can make flash message board! ", haha haha--ay! What are you smiling about? I'm happy because I finally fooled you again, what are you laughing at? Look at our "lv_shiyan.asp" file, we have already written an ASP statement Ah, how still do not need IIS support can be directly in the flash editing environment to display it? is our Flash software running under IIS? Of course it's impossible. Hey: Well, no headaches everyone, in fact, the first time we put "lv_shiyan.txt" directly into the "lv_shiyan.asp" and run the film test, "Lv_shiyan.asp" in the data has been read into memory, and later, although we put "lv_ Shiyan.asp "In the content of ASP-specific output format, but because the file name does not change, run the movie test, Flash will be directly from memory to extract the content has been stored, and the last content is not required to IIS can be displayed, just happen to:

So how do you show new content? We just need to use the random function to the ASP to pass a meaningless variable is OK. We can change the statement that loads the ASP into the following way:

Shiyan_lv.load ("lv_shiyan.asp?bianliang=" +random (9999));

Now test the movie again in Flash and find the Output window to show the following tips:

Load failed
Error opening URL "File:///E|/flashlianxi/flash and ASP Practice/flash and ASP communication entry level tutorials/lv_shiyan.asp?bianliang=5624"

Well!? Is there a problem, not to say "Bianliang" has no practical significance? How can cause "Error opening URL"? Oh: In fact, said here, "no practical meaning" is for the ASP, more specifically, for the browser, remember me in the "★asp basic input and output" section of the Get delivery mode? If you forget for a while, you can go back and review. In fact, my suggestion is that you follow my progress to thoroughly understand each section, and so I finished, and then read through the side, and then you can do it yourself a message board. Then, since this "no practical meaning" is not for Flash, the Flash must not know, it will be "lv_shiyan.asp?bianliang=5624" as a filename, so of course, can not find this file.

It's time to clear the clouds and see the sunny days. Now we first add a dynamic text field in "Lv_shiyan.fla" and name it "Wenben_txt", then change the output code so that what is displayed in the Output window is displayed in the dynamic text field, and the change is to replace the first line of code with the second line:
Trace ("The textual content that you load is:" +neirong1+ "/" +neirong2+ "/" +neirong3 ");
Wenben_txt.text = neirong1+ "/" +neirong2+ "/" +NEIRONG3;
OK, finally publish "lv_shiyan.swf" and "lv_shiyan.html" to the same folder as the ASP file, and run "lv_shiyan.html" under IIS, you will see the SWF's dynamic text field display "Conquer Loadvar Class!" /victory over asp! /I can make flash message board! Now you can finally cheer up: we can finally put the content of ASP output in the flash, haha ...

Drink water first, blink fast two hours, how I write so slow ah, come on! The above is actually the ASP how to pass data to flash, this is not the real meaning of the interaction, interaction is the interaction between the two sides, now we have to see how flash to the ASP to pass data. Before this, please make sure you remember how the ASP received a variable in the "basic Input and output" section of "★asp", and we also used the "asp_jichu.asp" file in that section, where I did the "Neirong_" directly in the browser address bar via get way Wangzhi, and the test proves that the ASP did receive the value of the variable through request. Now we're going to let Flash do the address bar, and we're going to assign "Neirong_wangzhi" through flash.

before we begin, we have the following work to be done :

1, add another button in "Lv_shiyan.fla", named "Tijiao_btn".
2, change the "wenben_txt" Dynamic text field into the Input text field, cancel the HTML output, and rename it to: "Shuru_txt".
3, add a dynamic text field, used to receive the data from the ASP, named: "Shuchu_txt."
4, change the contents of "asp_jichu.asp" to the following:
Basic input and output <br> in ★asp
<% @LANGUAGE = "JAVASCRIPT"%>
<%
var Neirong;
Get the variables passed from flash
Neirong=request ("Neirong_flash");
Output to return to the contents of Flash
Response.Write ("&fanhui_asp= ha, you entered the content has been passed to the ASP, and has been returned from the ASP to Flash, you enter the content is:" +neirong);
%>
5, and finally the "Lv_shiyan.fla" in the code to the following content:
System.usecodepage = true;
var shiyan_lv = new Loadvars ();
Tijiao_btn.onrelease = function () {
Get the text you entered and record the content in the variable "Neirong_flash"
This is equivalent to having the variable "Neirong_flash" assigned
Shiyan_lv.neirong_flash = Shuru_txt.text;
All the variables stored in the LV object are passed to the ASP, but here we only set one, which is our "Neirong_flash"
After the successful delivery to the ASP, we also store the variables passed back from the ASP in the "Shiyan_lv" object
Shiyan_lv.sendandload ("asp_jichu.asp?bianliang=" +random (9999), SHIYAN_LV, "post");
};
Shiyan_lv.onload = function (Chenggong) {
if (Chenggong) {
Output text content
Shuchu_txt.text = shiyan_lv.fanhui_asp;
} else {
Output text content
Shuchu_txt.text = "Load Failed";
}
};

It's a lot of things to change. First do not speak the code, the direct release test, first has an intuitive understanding: in the Input text box input "I want to do flash message board", and then click the Submit button, you will see in the dynamic text box "Shuchu_txt" will immediately show: "Ha, You have entered the content has been passed to the ASP, and has returned from the ASP to Flash, you entered the content is: I want to do flash message board. The source file for this example can also be downloaded directly below.

Tip : If you click Refresh directly in the browser to not display the updated page and content correctly, please set IE browser as follows: → tools →internet options → settings → check "Every time you visit this page". This way we can then use the refresh directly to test, no longer worry about the IIS refresh and memory problems.

Finally, we will make a systematic analysis of the data flow in the test process :

    • User in the Flash input text box "Shuru_txt" type "I want to make flash message Board"
    • Through "Shiyan_lv.neirong_flash = Shuru_txt.text;" This code stores the user input in the "Neirong_flash" of the "Shiyan_lv" object
    • Passes the variable that it records to the specified ASP file through the "Sendandload" method of the "Shiyan_lv" object
    • "Neirong=request" ("Neirong_flash") is passed in the ASP file; This code gets the variable "Neirong_flash" passed from the Flash's "Shiyan_lv" object.
    • The Write method that invokes the response object after the ASP receives the variable content outputs the variable/value matching format to output the new contents of the flash that need to be returned
    • Flash has specified "SHIYAN_LV" itself to receive returned content when it calls the "Sendandload" method of the "Shiyan_lv" object to send a variable
    • "SHIYAN_LV" receives the newly returned content and stores the new content in "shiyan_lv.fanhui_asp" according to its "variable/value" pairing format
    • When the newly returned content is loaded in Flash, the OnLoad event for the "Shiyan_lv" object is invoked
    • Through "shuchu_txt.text = shiyan_lv.fanhui_asp;" This code displays the newly returned content in the output text box
      After this analysis, I believe that we should be able to have a rational understanding of the overall.

Finally, the points to be highlighted are :

    • The LV object sends all the variables stored in the LV object to the ASP when it calls the "Sendandload" method to send the variable.
    • When the ASP receives the variable passed by the LV object, it is only necessary to use the same variable name as in Flash in the request.
    • ASP output data must use the "variable/value" matching format, because only the output in this format, flash can be like processing txt, the ASP output data are recorded in a number of variables and stored in the designated to receive data in the LV object.
    • If we want to pass two or more variables to flash, we can write this in Flash: (to pass three variables for example)
      Shiyan_lv.bianliang1_flash= "Bianliang1";
      Shiyan_lv.bianliang2_flash= "Bianliang2";
      Shiyan_lv.bianliang3_flash= "Bianliang3";
      Shiyan_lv.sendandload ("asp_jichu.asp?bianliang=" +random (9999), SHIYAN_LV, "post");
    • When you pass a large amount of data, you must use the Post method.
    • If we want to receive more than one variable from flash in Flash, just click on the TXT approach to the OK.

Shout, catch a breath, this section finally finished. Originally planned to write the next direct "LV and ASP comprehensive use", but now think, it is necessary to add a section of "ASP Operational database", please continue to focus. There is any good opinion can also mention, hope that through the joint efforts of everyone let this tutorial can let rookie with the fastest speed, the least obstacles to produce their own real flash walls.

Remember the meaningless variable "Bianliang" we passed to the ASP? Since we have passed to the ASP, the ASP should be able to receive, interested friends in the "asp_jichu.asp" in addition to a statement to receive variables, test the effect. The modified ASP code is as follows:

Basic input and output <br> in ★asp
<% @LANGUAGE = "JAVASCRIPT"%>
<%
var Neirong;
var Canshu;
Get the contents of a variable in the URL
Neirong=request ("Neirong_flash");
To get meaningless arguments
Canshu=request ("Bianliang");
Show variable Content
Response.Write ("&fanhui_asp= ha, you entered the content has been passed to the ASP, and has returned from the ASP to Flash, you entered the content is:" +neirong+. The meaningless parameters you pass are: "+canshu";
%>

After the test found that really can receive a number, and each point to submit this number is almost the same, change between 0-9999. It seems that we do receive the Flash pass over the random variable, can be passed, the address bar How did not show it? Because this time we used a "POST" pass, which is used to pass a lot of data, and it doesn't show the variable and its value in the Address bar.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.