---
title: "flex文件上传"
author: "quliangjun"
date: "2011-10-13T11:25:26Z"
tags: []
categories: ["移动开发"]
slug: "flex"
draft: false
lastmod: "2011-10-13T11:25:26Z"
id: 1193985
url: "https://quliangjun.iteye.com/blog/1193985"
---

本文讲的是通过Blazeds实现Flex文件上传，关于Blazeds的配置在这里就不赘述了，下面直接开讲。　　   
　说到Flex上传文件，就不得不用到FileReference了，下图是FileReference的属性：   
　可见data属性是ByteArray格式的，由其可以想到如果我们要上传文件，必然是将文件已比特流的形式通过网络上传到服务器。既然如此，我们就在服务器端写一个接受数据的JAVA类UploadFile。   
UploadFile.java   
1 package upload;   
2   
3 import java.io.File;   
4 import java.io.FileOutputStream;   
5   
6 public class UploadFile {   
7 public void uploadFile(byte\[] content, String fileName)   
8 throws Exception{   
9 File file = new File("路径"+fileName); //路径是你存文件的路径 比如c:\\upload\\  
10 FileOutputStream stream = new FileOutputStream(file);   
11 if(content != null)   
12 stream.write(content);   
13 stream.close();   
14 }   
15 }   
　　我在做的时候新建文件路径使用的是相对路径，在部署到blazeds之后发现文件总是上传不成功，改成绝对路径之后问题解决。其次，我用的 Flex IDE是Flash Builder4(从4代开始不再叫Flex Builder)，经常莫名其妙地出现上传文件的data属性为null的情况，因此加了一个判断content是否为null的语句，各位可以忽略。   
　　做完服务器的接收部分了，现在可以开始写我们的上传代码了。 

PopUpImage.mxml   
&lt;?xml version="1.0" encoding="utf-8"?&gt;  
&lt;s:Application xmlns:fx="[http://ns.adobe.com/mxml/2009](http://ns.adobe.com/mxml/2009)"  
      xmlns:s="library://ns.adobe.com/flex/spark"  
      xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"  
      currentState="pre\_upload"  
      creationComplete="init()"&gt;  
 &lt;s:layout&gt;  
  &lt;s:BasicLayout/&gt;  
 &lt;/s:layout&gt;  
 &lt;fx:Script&gt;  
  &lt;![CDATA[  
   //import mx.events.ImageUploadedEvent;  
   import flash.events.Event;  
   import flash.net.FileFilter;  
   import flash.net.FileReference;  
   import mx.controls.Alert;  
   import mx.managers.PopUpManager;  
   import mx.rpc.events.FaultEvent;  
   import mx.rpc.events.ResultEvent;  
   import mx.rpc.remoting.mxml.RemoteObject;  
     
   private var fileRef:FileReference = new FileReference();  
     
   private var upload:RemoteObject = new RemoteObject();  
   private function init():void{  
    upload.destination="UploadFile";  
   }  
     
   private function pickFile(evt:MouseEvent):void  
   {  
    var imageTypes:FileFilter = new FileFilter("图片 (\*.jpg, \*.jpeg, \*.gif,\*.png)", "\*.jpg; \*.jpeg; \*.gif; \*.png");  
    var allTypes:Array = new Array(imageTypes);  
    fileRef.addEventListener(Event.SELECT, selectHandler);  
    fileRef.addEventListener(ProgressEvent.PROGRESS, progressHandler);  
    fileRef.addEventListener(Event.COMPLETE, completeHandler);  
    try{  
     fileRef.browse(allTypes);  
    }catch (error:Error){  
     trace("Unable to browse for files."+error.toString());  
    }  
   }  
     
   private function progressHandler(evt:ProgressEvent):void  
   {  
    lb\_progress.text = " 已上传 " + (evt.bytesLoaded/1024).toFixed(2)+ " K，共 " + (evt.bytesTotal/1024).toFixed(2) + " K";  
    var proc: uint = evt.bytesLoaded / evt.bytesTotal * 100;  
    progress.setProgress(proc, 100);  
    progress.label= "当前进度: " + " " + proc + "%";  
   }  
     
   private function selectHandler(evt:Event):void  
   {  
    currentState = "uploading";  
    fileRef.load();  
   }  
     
   private function completeHandler(evt:Event):void{  
    currentState = "post\_upload";  
    upload.uploadFile(fileRef.data,fileRef.name);  
    image\_post\_upload.source = fileRef.data;  
   }  
     
  ]]&gt;  
 &lt;/fx:Script&gt;  
 &lt;s:states&gt;  
  &lt;s:State name = "pre\_upload"/&gt;  
  &lt;s:State name = "uploading"/&gt;  
  &lt;s:State name = "post\_upload"/&gt;  
 &lt;/s:states&gt;  
 &lt;s:Button includeIn="pre\_upload" label="从电脑上上传图片" width="119" click="pickFile(event)" horizontalCenter="-1" y="29"/&gt;  
 &lt;mx:ProgressBar id="progress" includeIn="uploading" x="43" y="43" width="153"/&gt;  
 &lt;s:Label id="lb\_progress" includeIn="uploading" x="45" y="10" width="149" height="25"/&gt;  
 &lt;s:Label id="lb\_post\_upload" includeIn="post\_upload" x="108" y="21" width="124" height="32" fontSize="16" text="upload success!" fontFamily="Arial" color="#3374DE"/&gt;  
 &lt;mx:Image id="image\_post\_upload" includeIn="post\_upload" x="10" y="10" width="67" height="67"/&gt;  
&lt;/s:Application&gt;  
　   组件有三个状态，分别为upload前、中、后期。   
　　这里重点说的是RemoteObject，需要在remoting-config.xml文件中进行配置。 

remoting-config.xml的位置是在Web-inf\\flex里  另外编译的时候要加上参数指向这个文件 参考我的文章

[http://hi.baidu.com/caidows/blog/item/19366bc61e79f9089c163de7.html](http://hi.baidu.com/caidows/blog/item/19366bc61e79f9089c163de7.html)

remoting-config.xml添加  
1 &lt;destination id="uploadFile" channels="my-amf"&gt;   
2 &lt;properties&gt;   
3 &lt;source&gt;upload.UploadFile&lt;/source&gt;   
4 &lt;/properties&gt;   
5 &lt;/destination&gt;   
以上就是一个完整的Flex通过Blazeds上传文件的过程。

原作者：[mikel](http://www.douban.com/people/Mikel/) 转自：[http://www.douban.com/group/topic/15982509/](http://www.douban.com/group/topic/15982509/)