Flex调用JavaScript获取文件路径

Flex的Web中有FileReference的类可以对文件操作,实现上传、下载的功能,但是没有办法获取到文件的路径。

普遍的方法是Flex调用JavaScript的文件浏览功能来获取文件路径。

1、Flex端,首先需要在项目(test)的“Initialize”或“creationComplete”下注册事件
 //调Js获取选择图片的路径(第一个getPath为Js中方法,后一个为Flex端对应接收方法)

private function init():void
{

     ExternalInterface.addCallback("getPath",getPath);

}

2、Flex端,创建getPath方法

//此时的path为已经获得了的文件路径

public function getPath(path:String):void
{

     Alert.show("文件的路径为:" + path);

}

3、JavaScript端,在项目“html-template => history => index.template.html的<body></body>中,添加控件

注:此处多加一个<form>控件,是因为有时候在连续调用两次文件浏览控件时,会出现上一个选择文件记录没有被清空的bug。

因此每当调用文件浏览控件时,都要reset()一下

<body>

        <form id="tempForm" name="form1">
        <input type="file" id="fileInput" style="display:none" onchange="OnFileChange()"/><!--fileInput控件-->
        </form>

</body>

4、JavaScript端,在项目“html-template => history => index.template.html的<head></head>中,添加方法

<head>

<script language="JavaScript" type="text/javascript">
//关键代码
function Browser()
{

//当选择文件
document.getElementById("fileInput").click();

//重设控件信息
tempForm.reset();
}
function OnFileChange()
{

//获取选择文件的路径
test.getPath(document.getElementById("fileInput").value);
}
</script>

</head>

原文地址:https://www.cnblogs.com/giser-s/p/3183527.html