Filereference path acquisition

Source: Internet
Author: User
Transfer to the subject.
In fact, I have always ignored this problem. I came from the Flash MX era. After mx2004 came out, as didn't learn it seriously. Till now, I have hardly used many new features after player7, such as the Tween class and filters class.
The filereference class has not aroused my interest for a long time. However, after reading the sample provided by flash 8, I found it useful. You can select a local image, then it is displayed in flash.
However, it is very troublesome to display a local image through the network after uploading and downloading it, I want to demonstrate the filereference class upload.) I decided to modify it and create a local image browser. Flash 8 supports JPG, GIF, and PNG types of images, which can be used to change the image.
However, I was disappointed with everything. After a long time, I could not read the image directly, because the filereference class could not get the file path and could only get the file name, there is no way to read the corresponding image through the image path using loadmovie or mcloader class. However, it's just a little bit worse.
Although the Help file does not mention how to obtain the file path, I still don't have to worry about it. I always feel that there are some, so I wrote the following code for testing:
Import flash.net. filereference;
VaR fr: filereference = new filereference ();
Fr. Browse ();
VaR listener: Object = new object ();
Fr. addlistener (listener)
Listener. onselect = function (File: filereference ){
For (var I in file ){
Trace (I + ":" + file [I]);
}
};
Output result after selecting a file
_ Listeners: [object]
Creator: Undefined
Creationdate: Thu Feb 15 20:58:03 GMT + 0800 2007
Modificationdate: Thu Feb 15 20:58:04 GMT + 0800 2007
Size: 3753620
Type:. MP3
Name: 2007425171248133
Dizzy, the path is not available for enumeration with for, it seems that this is a complete despair. You don't have to limit the security, do you? Macromedia?
This also means that the file object in the Code cannot be manually constructed by itself and must be constructed by using the Browse method of filereference. Because it is impossible to get the file you browsed only through the output result, because there is no path.
Search engines are used to find the answer to how to obtain the file path.
Because there are not many people asking this question, this issue has been stranded and has never been taken care.
Later, Zhu Gao wanted to be a player that could play local music, because he took a fancy to the filereference class of Flash 8. However, when I told the truth that the filereference class could not obtain the file path, his idea was dispelled just like the idea of creating an image browser.

Someone has recently asked this question. It seems that it is time to study it.
Http://bbs.blueidea.com/thread-2721771-1-1.html

To solve this problem in filereference, at least for the moment, Flash is useless. Since the file domain in the webpage has a complete path, why don't we apply it?

Therefore, the communication between flash and JS is mainly used to solve this problem. The previous tutorials used fscommand to implement externalinterface. Therefore, next, we will first introduce the usage of the externalinterface class (I believe most people will, to put it simply), and then a method in the file field that is equivalent to browse in filereference, and an event similar to onselect in filereference.

Demo address: http://hbro.cincn.com/HBrO/course/fileField/fileField.html

Before entering the instance description, we will first introduce the following content:
1 Use of the externalinterface class
The communication between flash and JS on the webpage is used as an example. First, in flash, the externalinterface class is in the Flash .net package. Generally, this path is not input for later calling. As will be added first
Import flash.net. filereference
After this sentence is added, you do not need to input flash.net again when using this class.

Next, you can use the call method to call JS functions in flash.
Call (methodname: String, [parameter1: object])
For ease of demonstration, I use a built-in alert method of Js for testing.
Create a flash file, save it as externalinterface. fla, and enter the following code:
Import flash.net. filereference;
Externalinterface. Call ("alert", 1 );
Publish it and test the web page that you just generated under IIS. A dialog box of 1 is displayed. That is to say, Flash has implemented this step to call JS functions.
Then, the flash is controlled by Js. JS can call the functions in flash. Here, the externalinterface class also plays an important role.
To enable a function in flash to be called by JS, use externalinterface to register it:
Addcallback (methodname: String, instance: object, method: function): Boolean
Among them, methodname is the name called for JS, instance is the instance where the as function is located, and method is the name of the function in
Returns a Boolean value indicating whether the call is successful. However, the following example ignores the returned value.
In externalinterface. fla, append the Code:
Externalinterface. addcallback ("gotogoogle", this, goto_google );
Function goto_google (){
Geturl ("http://www.google.com", "_ blank ");
}
Then, press Ctrl + enter to generate the Flash video.
In externalinterface.html, locate </body> and add the code before it:
<A href = "#" onclick = "externalinterface. gotogoogle"> gotogoogle </a>
Save the HTML and run it. Click the gotogoogle button to open the Google homepage. That is to say, the goto_google function in as has been called through Js.

 

2. Obtain the file path in the webpage file domain
If you want to use a file domain, you must obtain the path of the file domain. The Value Attribute of the file domain is used, which is the same as the method for obtaining the text domain. However, this attribute seems to have to be read, but it is not easy to set. If it is set, the text box in the file domain does not seem to change through file. value = "XXX. There may be a function for setting the value, but it hasn't been found yet.
The test code is relatively simple.
<Input type = "file" id = "A"/> <br/>
<Input type = "button" onclick = "alert (A. Value)" value = 'view'>
A is the ID of the file field. After you click a button, the file path is displayed in the dialog box.

3. Replace the "Browse" button
Because it needs to be called in flash, when you use it, you do not need to click the Browse button next to the file domain. Then, how can we replace this button so that we can open the local browser window and set the file Domain value?
To find the answer to this question, I can say that it has been difficult. Although this post solved the problem last night, the filereference class and file domain problems have plagued me for a long time.
In the book about creating web pages, when it comes to the insertion of form elements, many of them detail how to set values and attributes for various form elements. In Dreamweaver, any attribute panel is clearly described. Such as text fields, initial values, and the maximum number of characters. If the book contains JS content, how to set text fields using JS will also be explained accordingly.
However, although the file domain cannot be avoided by the author, the introduction of the file domain is always a few words, and it is all about how to insert the file. The setting is rarely explained in depth. Likewise, the author tries to omit the file domain content in JavaScript. Some make readers feel comprehensive through comprehensive investigation form examples, instead of investigating how to use the file domain, some are explaining the settings of a certain element, and they are asking the readers to give an inverse explanation, because the frequency of text domain usage is not high, after reading the book, the reader will not report any problems to the author. But in fact, they still do not know much about the usage of text fields.
However, this problem cannot be completely attributed to the author. After all, the file domain has its own particularity.
The form is initially used to submit data to the user to the background. In forms that do not contain file fields, the submitted data is basically an ASCII string, or the submitted data is opened in Notepad without garbled characters. When the background obtains the data, it usually uses request ("XXX") to obtain and process the data. However, the data submitted by the file domain is different. It contains every byte of the corresponding file in the file domain. It may contain binary data and cannot be recognized by notepad, requests alone cannot be a task, and the upload component is also required. asp may use a component-less upload class. Although request. binaryread and request. binarywrite are often used to process binary data, they are more complex than forms without file fields. In the book or tutorial of a webpage program, submit a form containing a file field to explain it independently, which indicates that the file field has special features in the background processing.

With the development of JS client scripts and the increasing burden on servers, if the form data is transmitted directly, the server will crash more easily. Therefore, the form content is often processed at the front end to reduce unnecessary transmission. In some simple applications, even all form data is not submitted to the background, and everything serves the foreground. In this case, the file domain is not widely used. If you only consider the foreground, the file domain only has more browsing functions than the text domain, you can get some specific strings, the objective of obtaining these strings is to read or write files. However, reading and writing files on the front-end of a Web page usually disables the security of the browser. Therefore, the file domain loses its unique value, which is no different from the text domain.
Therefore, the use of file domains is very easy to ignore, especially for foreground scripts. Looking for this "browsing" method can be a headache for me. I tried to use the for in enumeration, but unfortunately, I can only get attributes and events without functions. Therefore, different search engines cannot find the answer by using different keywords.
After an hour of searching and trying, I found the answer to the question in a non-awesome answer in a post discussing CSS in the foreground area of the classic forum more than 600 days ago.
Http://bbs.blueidea.com/thread-1803020-1-1.html
The marvellous on the third floor provides a method to Implement CSS by hiding file fields and adding other form controls.
<Input type = file name = J style = "display: none;" onchange = "ye. value = value "> <input name = ye style =" color: green; Border: 1px solid green "> <input type = button value =" file "onclick = J. click () style = "border: 1px solid green">
Although CSS is discussed, it achieves the desired effect because it controls the file fields externally. Thank you very much for the answers provided by this moderator.
When you press a common button, call the J. Click () method. J is the ID of the file field. Obviously, click is the browsing function of the file field.
With his code, everything below will be well solved.

4. The file field is equivalent to the onselect event in the filereference class.
With the above Code, it is not difficult to find this event. Because the displayed text box is not the text box of the file domain itself, it is another text field created by itself. After you click the self-created button during the code test, if you select a file, the file path can also be displayed in your own text domain. the ID of the text domain is ye. the onchange event of value = value is naturally the event that is returned after the file is selected.

With this model, we find that all the content we need in the file domain can be obtained, as long as the content is passed to the as through the externalinterface class, you can obtain the file path in flash.

The procedure is as follows:
1. Create a dynamic text or input text in flash to display the path, and then create a browse button.
2. Create a hidden file domain in the webpage and call it for Js.
3. Click the Browse button of flash to call a JS function through externalinterface. Call. This function contains the click method of the webpage file domain.
4. Open the Browse local file dialog box, select the file, respond to the onchange event, and call a function in flash. This function contains the statement for setting the text content of the dynamic text box in flash.

Follow these steps to create the file domain of the Flash version.
1. Create a New FLASH file and save it as filefield. Fla.
Then, put a dynamic text box or input text box in the scenario, the Instance name is filename, and then a button, the Instance name is browse_btn.

2. Select "file"-"Release Settings", click the release button, and remove the HTML marker in the format tab (no fscommand is needed now, so you don't need to follow up as before ).
Then, in the filefield.flag directory, open filefield.html with the help of the notebook, find the </body>, and enter the following code:
<Input type = "file" id = "myfile" onchange = "givevaluetoflash (myfile. Value)" style = "visibility: hidden">
Here there is a givevaluetoflash, that is to say, after the file domain is selected, this function is called. This function is defined later, and the Value Attribute of the file domain will be passed to flash.

3. Return to filefield. Fla and enter the code in the frame:
Import flash. External. externalinterface; // load the externalinterface class

Browse_btn.onrelease = function (){
Externalinterface. Call ("Browse"); // enables flash to call the JS browse function. This function is defined in other languages and is used to open the browser dialog box.
};

To filefield.html, enter the code before </body>:
<Script language = "JavaScript">
Function browse (){
Myfile. Click (); // The browse function called by flash can open the browsing dialog box.
}
</SCRIPT>

4. After function browse () {}, append the givevaluetoflash function responded by the onchange event:
Function givevaluetoflash (VAL ){
Filefield. setvalue (VAL); // filefield is the default swf id of the HTML generated by flash. In this case, the setvalue method of filefield is called. However, this is not included in flash, you need to use addcallback for registration.
}

Convert to filefield. Fla and append the Code:
Externalinterface. addcallback ("setvalue", this, settextvalue); // make setvalue a method of flash. settextvalue indicates that when the setvalue method of Flash is called in a webpage or other container, settextvalue in flash will be called.
// Therefore, the following function is called externally.
Function settextvalue (VAL: string): void {
Filename. Text = val; // set the value of dynamic text or input text to the value attribute of the file field.
}

 

In this step, the file path can be obtained by flash. However, we can see that the filereference class is not involved at all. Therefore, all the methods and attributes in it are, you cannot use events. Therefore, it would be perfect if it could be encapsulated into a class. Unfortunately, it needs to interact with JS Code. js exists in webpages and is not easy to put in the as class. However, after reading the abc12hjc method to keep HTML clean, you can also try to use the geturl ("javascript: XXX") method to write some JavaScript code to the class. In addition, the externalinterface class greatly reduces the code of the webpage, and the writing class can still be tried.
If you want to upload a file using this file field, you need to submit the data to the HTML. However, page Jump is required for HTML data submission. How can I achieve non-jump page upload like flash? As mentioned above, mirycat uses a hidden IFRAME for upload. I didn't implement it in the demo address, but I will continue to add it.
I wanted to continue writing the upload tutorial. I didn't expect a bunch of replies that could not be run under ff so quickly.
Cry, And let FF go. even the click method of the file domain is unavailable. what else can I set the flash parameter? Today, I only know how to make it effective under ff. in my opinion, FF and IE have a lot of differences, which is really a headache.
This time, I finally got a chance to run it under ff, but there are still some problems. Even if you step on it, I will find a lot of problems.
Http://hbro.cincn.com/HBrO/course/fileField (FF)/filefield.html

Classic Forum details:

Http://bbs.blueidea.com/thread-2721826-1-1.html

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.