天天看点

Uploading files in Flex using the FileReference class

The following example shows how you can use the FileReference class’s <code>browse()</code> method to allow users to select and upload a single file to a Web server.

If you want to allow users to upload multiple files at once, you would use the FileReferenceList class instead of FileReference.

&lt;?xml version="1.0" encoding="utf-8"?&gt;

&lt;!-- http://blog.flexexamples.com/2007/09/21/uploading-files-in-flex-using-the-filereference-class/ --&gt;

&lt;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"

        layout="vertical"

        verticalAlign="middle"

        backgroundColor="white"

        creationComplete="init();"&gt;

    &lt;mx:Script&gt;

        &lt;![CDATA[

            private var fileRef:FileReference;

            private const FILE_UPLOAD_URL:String = "http://www.YOUR-WEBSITE-HERE.com/fileref/uploader.cfm";

            private function init():void {

                fileRef = new FileReference();

                fileRef.addEventListener(Event.SELECT, fileRef_select);

                fileRef.addEventListener(ProgressEvent.PROGRESS, fileRef_progress);

                fileRef.addEventListener(Event.COMPLETE, fileRef_complete);

            }

            private function browseAndUpload():void {

                fileRef.browse();

                message.text = "";

            private function fileRef_select(evt:Event):void {

                try {

                    message.text = "size (bytes): " + numberFormatter.format(fileRef.size);

                    fileRef.upload(new URLRequest(FILE_UPLOAD_URL));

                } catch (err:Error) {

                    message.text = "ERROR: zero-byte file";

                }

            private function fileRef_progress(evt:ProgressEvent):void {

                progressBar.visible = true;

            private function fileRef_complete(evt:Event):void {

                message.text += " (complete)";

                progressBar.visible = false;

        ]]&gt;

    &lt;/mx:Script&gt;

    &lt;mx:NumberFormatter id="numberFormatter" /&gt;

    &lt;mx:Button label="Upload file"

            click="browseAndUpload();" /&gt;

    &lt;mx:Label id="message" /&gt;

    &lt;mx:ProgressBar id="progressBar"

            indeterminate="true"

            visible="false" /&gt;

&lt;/mx:Application&gt;

    本文转自 OldHawk  博客园博客,原文链接:http://www.cnblogs.com/taobataoma/archive/2008/01/13/1037007.html,如需转载请自行联系原作者

继续阅读