本文讲的是通过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
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="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()">
<s:layout>
<s:BasicLayout/>
</s:layout>
<fx:Script>
<![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;
}
]]>
</fx:Script>
<s:states>
<s:State name = "pre_upload"/>
<s:State name = "uploading"/>
<s:State name = "post_upload"/>
</s:states>
<s:Button includeIn="pre_upload" label="从电脑上上传图片" width="119" click="pickFile(event)" horizontalCenter="-1" y="29"/>
<mx:ProgressBar id="progress" includeIn="uploading" x="43" y="43" width="153"/>
<s:Label id="lb_progress" includeIn="uploading" x="45" y="10" width="149" height="25"/>
<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"/>
<mx:Image id="image_post_upload" includeIn="post_upload" x="10" y="10" width="67" height="67"/>
</s:Application>
组件有三个状态,分别为upload前、中、后期。
这里重点说的是RemoteObject,需要在remoting-config.xml文件中进行配置。
remoting-config.xml的位置是在Web-inf\flex里 另外编译的时候要加上参数指向这个文件 参考我的文章
http://hi.baidu.com/caidows/blog/item/19366bc61e79f9089c163de7.html
remoting-config.xml添加
1 <destination id="uploadFile" channels="my-amf">
2 <properties>
3 <source>upload.UploadFile</source>
4 </properties>
5 </destination>
以上就是一个完整的Flex通过Blazeds上传文件的过程。