简体   繁体   中英

why call flash function from javascript work but FileReference don't work in flash?

I need call flash function from javascript. I use flash.external and addCallback to do this. all things work well but when I use FileReference in my flash, function did not open my browser ...

please see below describtion:
I call my function in javascript with this code: <input type="button" value="Browse" onclick="sendToFlash('Hello World! from HTML');" /> <input type="button" value="Browse" onclick="sendToFlash('Hello World! from HTML');" /> you can see all my HTML as below:

<html>
<head>
<title>Upload test</title>
</head>
<script>
function hello (size) {
    alert ("size hast: " + size);
}

function sendToFlash(val){
    var flash = getFlashObject();
    flash.new_browser(val);
}

var flash_ID = "Movie2";
var flash_Obj = null;
function getFlashObject(){
    if (flash_Obj == null){
        var flashObj;
        if (navigator.appName.indexOf( "Microsoft" ) != -1){
           flashObj = window[flash_ID];
        } 
        else{
           flashObj = window.document[flash_ID];
        }
        flash_Obj = flashObj;
    }
    return flash_Obj;
}
</script>

  <body>
    <center>

    <embed width="560" height="410" type="application/x-shockwave-flash" 
    flashvars="sampleVars=loading vars from HTML" 
    salign="" allowscriptaccess="sameDomain" allowfullscreen="false" menu="true" name="Movie2" 
    bgcolor="#ffffff" devicefont="false" wmode="window" scale="showall" loop="true" play="true" 
    pluginspage="http://www.adobe.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" 
    quality="high" src="Movie2.swf">

    </center>

    <input type="button" value="Browse" onclick="sendToFlash('Hello World! from HTML');" />
  </body>
</html>

when I click Browse in html page, javascript call sendToFlash function and SendToFlash function send my string (Hello World! from HTML) to flash.
in flash I get this string with below code:

resultsTxtField.text = "";
uploadButton.onPress = function () {
   return browse_file("Hello World! from Flash");
}

import flash.external.*;
ExternalInterface.addCallback("new_browser", this, browse_file);


function browse_file (my_test_val) {
    _root.resultsTxtField.text = "val: " + my_test_val;
    import flash.net.FileReference;
    var fileTypes:Array = new Array();
    var imageTypes:Object = new Object();
    imageTypes.description = "Images (*.jpg, *.jpeg, *.gif, *.png)";
    imageTypes.extension = "*.jpg; *.jpeg; *.gif; *.png";
    fileTypes.push(imageTypes);

    var fileListener:Object = new Object();
    var btnListener:Object = new Object();
    var fileRef:FileReference = new FileReference();
    fileRef.addListener(fileListener);
    fileRef.browse(fileTypes);

    fileListener.onCancel = function(file:FileReference):Void
    {
      _root.resultsTxtField.text += "File Upload Cancelled\n";
    }

    fileListener.onSelect = function(file:FileReference):Void
    {
      _root.resultsTxtField.text += "File Selected: " + file.name + " file size: "+ file.size + " file type: " + file.type;
      getURL("javascript:hello("+file.size+");");
    }
}

I have only one Scene and this code is on root of this Scene. and I have one movie clip named uploadButton and has only a rectangle that work as button in this sample.
when you click on rectangle browse_file("Hello World! from Flash"); called and a browser open that you can select a photo to upload.
when you click on browse in html same process must do but as you see variable send to function but browser to select a photo did not open any more.
I try several ways. for example I set new function to open only picture browser or set new Scene or use gotoAndPlay and more but there is another problem.

you can download my source from below link:
http://www.4shared.com/zip/YTB8uJKE/flash_uploader.html
note that javascript onclick="sendToFlash('Hello World! from HTML');" don't work in direct opening. you must open it in localhost.

I'll be so so happy for any clue.
thanks so much
Reza Amya

You can't programmatically call browse() , it has to be from a mouse click inside Flash: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/FileReference.html#browse ()

In Flash Player 10 and Flash Player 9 Update 5, you can only call this method successfully in response to a user event (for example, in an event handler for a mouse click or keypress event). Otherwise, calling this method results in Flash Player throwing an Error exception.

after a day reading I know that it is impossible for security reasons.
then you can't open file reference with addCallback javascript code any way. for more information read fileReference browse from js, simulate keypress in flash workaround


Thanks again
Reza Amya

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM