如何在Flex 2中上传时获取文件的byteArray

时间:2011-04-12 04:33:14

标签: flex

     There is a WSDL which have one method which take byteArray & fileName(which i want to write) as a parameter to write a file in local system, the following is the code...

public void writeLocation(byte[] byteToWrite, String fileName) throws FileNotFoundException
{
    StringBuffer fileLocation = new StringBuffer("D:\\Products\\Device");
    fileLocation.append("\\"+fileName);
    File file = new File(fileLocation.toString());
    BufferedOutputStream bufferWriter = new BufferedOutputStream(new FileOutputStream(file));
    try 
    {
        bufferWriter.write(byteToWrite);
    }
    catch (IOException e) 
    {
        System.out.println("Error file Writing....");
        e.printStackTrace();
    }
    finally
    {
        try 
        {
            bufferWriter.close();
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }
    }
}

现在我正在使用flex 2&做以下事情:

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:WebService id = "byteCheckUpload" wsdl="http://localhost:8080/XFireTest/xfire/EmployeeService?wsdl" showBusyCursor="true">
        <mx:operation name="writeLocation" fault="writeLocationFH(event)" result="writeLocationRH()"/>
    <mx:WebService/>
<mx:Script>
    <![CDATA[
        import mx.controls.Alert;
        import mx.rpc.events.FaultEvent;

        private var refUploadFile : FileReference;

        public function uploadFile() : void
        {
            refUploadFile = new  FileReference(); 
            refUploadFile.browse(); 
            refUploadFile.addEventListener(Event.SELECT,onFileSelect); 
            refUploadFile.addEventListener(Event.COMPLETE,onFileComplete);  
        }

        public function onFileSelect(event : Event) : void
        {
            refUploadFile.load();
        }

        public function onFileComplete(event : Event) : void
        {
            refUploadFile = event.currentTarget as FileReference; 
            var data:ByteArray = new ByteArray(); 
            refUploadFile.data.readBytes(data,0,refUploadFile.data.length); 

            byteCheckUpload.writeLocation(data, refUploadFile.name) 
        }

        public function writeLocationFH(event : FaultEvent) : void
        {
            Alert.show(event.fault.faultString);    
        }

        public function writeLocationRH() : void
        {
            Alert.show("Please check the Location .... !!!");
        }
    ]]>
</mx:Script>
<mx:Button x="308" y="156" label="Click button to Upload a file" click="uploadFile();"/>

但是flex 2在我搜索时显示错误我在flex 2中发现没有“data”属性。所以我无法将所选对象创建为byteArray ..任何人都可以帮助我如何做到这一点..&amp;不想使用HTTPService(POST方法)..

请帮助..在此先感谢

1 个答案:

答案 0 :(得分:0)

FP 9中没有该功能。您需要target your application for flash player 10

这适用于Flex 3,因此它可能适用于旧版本的框架,也可能不适用。无论哪种方式,问题是您使用的是您正在使用的版本中没有的功能。