wangleifire 发表于 2013-1-29 07:41:11

flex读取本地文件的解决方案

鉴于adobe并没有提供FileReference对浏览的文件的完整路径的接口。
只能采用JS和fileinput控件来获取本地路径了。
mxml代码
<!--ReadLocalFile.mxml-->
<?xmlversion="1.0" encoding="utf-8"?>
<mx:Applicationxmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"creationComplete="init()">
<mx:Script>
     <![CDATA[
         
      private
functioninit():void
      {
             ExternalInterface.addCallback("OnFileChange",frSelectHandler);//注册JS回调
             btnBrowser.addEventListener(MouseEvent.CLICK,mouseClickHandler);
         }
      
      private functionmouseClickHandler(event:MouseEvent):void
         {
          ExternalInterface.call("Browser");//调用JS中Browser函数
         }
      
      private functionfrSelectHandler(path:String):void
      {
             imgTest.source = path;
         }
     ]]>
</mx:Script>
     <mx:Button id="btnBrowser"x="10" y="10" label="Button" />
     <mx:Image id="imgTest"   x="10"y="50"/>
</mx:Application>

html端代码
<!--ReadLocalFile.html -->
<html lang="en">
<head>
<metahttp-equiv="Content-Type" content="text/html; charset=utf-8" />
<linkrel="stylesheet" type="text/css" href="history/history.css"/>
<title></title>
<script src="AC_OETags.js"language="javascript"></script>
<script src="history/history.js"language="javascript"></script>
<style>
body { margin: 0px;overflow:hidden }
</style>
<script language="JavaScript"type="text/javascript">
var requiredMajorVersion = 9;
varrequiredMinorVersion = 0;
var requiredRevision = 28;
//关键代码
functionBrowser()
{
document.getElementById("fileInput").click();
}
functionOnFileChange()
{
thisMovie("ReadLocalFile").OnFileChange(document.getElementById("fileInput").value);
}
functionthisMovie(movieName) {
        if (navigator.appName.indexOf("Microsoft") !=-1) {
            return window;
        } else{
            return document;
        }
       }
</script>
</head>
<body scroll="no">
<inputtype="file" id="fileInput" style="display:none" onchange="OnFileChange()"/><!--fileInput控件-->
<script language="JavaScript" type="text/javascript">
varhasProductInstall = DetectFlashVer(6, 0, 65);
var hasRequestedVersion =DetectFlashVer(requiredMajorVersion, requiredMinorVersion,requiredRevision);
if ( hasProductInstall && !hasRequestedVersion ){
var MMPlayerType = (isIE == true) ? "ActiveX" : "PlugIn";
varMMredirectURL = window.location;
     document.title = document.title.slice(0,47) + " - Flash Player Installation";
     var MMdoctitle =document.title;
AC_FL_RunContent(
   "src","playerProductInstall",
   "FlashVars","MMredirectURL="+MMredirectURL+'&MMplayerType='+MMPlayerType+'&MMdoctitle='+MMdoctitle+"",
   "width","100%",
   "height", "100%",
   "align", "middle",
   "id","ReadLocalFile",
   "quality", "high",
   "bgcolor", "#869ca7",
   "name","ReadLocalFile",
   "allowScriptAccess","sameDomain",
   "type","application/x-shockwave-flash",
   "pluginspage", "http://www.adobe.com/go/getflashplayer"
);
}else if (hasRequestedVersion) {
AC_FL_RunContent(
  "src","ReadLocalFile",
  "width", "100%",
  "height", "100%",
  "align","middle",
  "id", "ReadLocalFile",
  "quality", "high",
  "bgcolor","#869ca7",
  "name", "ReadLocalFile",
     "allowScriptAccess","sameDomain",
  "type","application/x-shockwave-flash",
  "pluginspage", "http://www.adobe.com/go/getflashplayer"
);
   }else {   // flash is too old or we can't detect the plugin
     varalternateContent = 'Alternate HTML content should be placed here. '
  +'This content requires the Adobe Flash Player. '
     + '<ahref=http://www.adobe.com/go/getflash/>GetFlash</a>';
     document.write(alternateContent);   // insert non-flashcontent
   }
</script>
<noscript>
  <objectclassid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
  id="ReadLocalFile"width="100%" height="100%"
  codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">
     <param name="movie" value="ReadLocalFile.swf" />
  <paramname="quality" value="high" />
  <param name="bgcolor" value="#869ca7"/>
  <param name="allowScriptAccess" value="sameDomain" />
     <embed src="ReadLocalFile.swf" quality="high"bgcolor="#869ca7"
     width="100%" height="100%" name="ReadLocalFile"align="middle"
     play="true"
     loop="false"
     quality="high"
     allowScriptAccess="sameDomain"
     type="application/x-shockwave-flash"
     pluginspage="http://www.adobe.com/go/getflashplayer">
     </embed>
</object>
</noscript>
</body>
</html>
页: [1]
查看完整版本: flex读取本地文件的解决方案