flex文件下载
flex与java后端结合,实现文件下载的原理:
1、flex调用相应的java函数得到服务器上的文件地址。
2、利用flex里的封装类FileReference。 使用download(URLRequest, defaultFilename) 将服务器上的文件下载的客户端本地。
注意注意 :这里的URLRequest.url="...." 不能有中文。否则会出现2038错误I/O文件错误。
3.可以给fileReference的对象添加事件,。监视文件是否下载完成。
具体源码:
<?xml version="1.0" encoding="utf-8"?>
<utils:BaseModule xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:utils="utils.*"
xmlns:commons="commons.*"
width="100%" height="100%"
creationComplete="basemodule1_creationCompleteHandler(event)"
layout="absolute">
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.rpc.events.ResultEvent;
import mx.collections.ArrayCollection;
import events.SysEvents;
import mx.controls.Alert;
import mx.events.FlexEvent;
import mx.rpc.events.ResultEvent;
[Bindable]
private var comboDatas:ArrayCollection = new ArrayCollection([{label:"根据查询结果"},{label:""},{label:"选项3"},{label:"选项4"}]);
[Bindable]
public var dat:ArrayCollection=new ArrayCollection();
private var resultDownUrl:String="";
private var filerefer:FileReference=new FileReference();
protected function basemodule1_creationCompleteHandler(event:FlexEvent):void
{
// TODO Auto-generated method stub
//获得文件到出路径
contcatExport.addEventListener(ResultEvent.RESULT,getExportUrl);
contcatExport.exportContactDirectory("","","");
//下载完成后删除服务器文件
filerefer.addEventListener(Event.COMPLETE, onDownloadComplete);
}
private function getExportUrl(evs:ResultEvent):void{
resultDownUrl=evs.result.toString();
}
protected function onDownloadComplete(e:Event):void {
var file:FileReference = e.target as FileReference;
Alert.show("文件下载成功! 其中文件名: " + file.name + " 文件大小:" + file.size);
/* statisDownload.exportDel(resultDownUrl); */
}
private function init():void{
}
protected function group1_clickHandler(event:MouseEvent):void
{
var downloadURL:URLRequest=new URLRequest();
downloadURL.url =resultDownUrl; //"report/201310161910736.zip";
filerefer.download(downloadURL);
}
protected function getDownloadURL(evs:ResultEvent):void{
var result:Object=JSON.parse(evs.result.toString());
}
protected function labelImport_clickHandler(event:MouseEvent):void
{
}
protected function labelCancel_clickHandler(event:MouseEvent):void
{
// TODO Auto-generated method stub
close_Cli();
}
public function close_Cli():void{
SysEvents.dispatch(SysEvents.REMOVE_ALL_MODULE,"");
}
]]>
</fx:Script>
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
<s:RemoteObject id="contcatExport" destination="contcatExport"/>
</fx:Declarations>
<s:Group width="100%" height="100%">
<s:Rect width="100%" height="100%">
<s:fill>
<s:SolidColor alpha="0.5" color="#000000"/>
</s:fill>
</s:Rect>
</s:Group>
<fx:Declarations>
<mx:HTTPService id="sevlet" resultFormat="e4x"/>
</fx:Declarations>
<s:HGroup width="100%" height="100%" horizontalAlign="center" verticalAlign="middle">
<commons:myPop clo="{close_Cli}" iconPath="assets/requirM/7_.png" labelStr="目录导出"
skinClass="Skins.myPopSkin">
<s:VGroup left="5" right="5" top="10" bottom="5" width="500" height="400" gap="10">
<s:HGroup width="100%" paddingLeft="20" verticalAlign="middle">
<mx:ComboBox id="items" x="10" y="34" width="228" dataProvider="{comboDatas}" selectedIndex="0"/>
</s:HGroup>
<s:HGroup width="100%" paddingLeft="20" verticalAlign="middle">
<s:Label width="40%" text="文件组织方式: "/>
<s:Label width="40%" text="大文件" color="red"/>
</s:HGroup>
<s:HGroup width="100%" gap="40" horizontalAlign="center" verticalAlign="middle">
<s:Group width="109" height="35" buttonMode="true"
click="group1_clickHandler(event)">
<s:Image source="assets/pop/25.png"/>
<s:HGroup width="109" height="35" horizontalAlign="center"
verticalAlign="middle">
<s:Label text="确定"/>
</s:HGroup>
</s:Group>
<s:Group width="109" height="35" buttonMode="true">
<s:Image source="assets/pop/25_.png"/>
<s:HGroup width="109" height="35" horizontalAlign="center"
verticalAlign="middle">
<s:Label text="取消" click="close_Cli()"/>
</s:HGroup>
</s:Group>
</s:HGroup>
</s:VGroup>
</commons:myPop>
</s:HGroup>
</utils:BaseModule><?xml version="1.0" encoding="utf-8"?>
<utils:BaseModule xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:utils="utils.*"
xmlns:commons="commons.*"
width="100%" height="100%"
creationComplete="basemodule1_creationCompleteHandler(event)"
layout="absolute">
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.rpc.events.ResultEvent;
import mx.collections.ArrayCollection;
import events.SysEvents;
import mx.controls.Alert;
import mx.events.FlexEvent;
import mx.rpc.events.ResultEvent;
[Bindable]
private var comboDatas:ArrayCollection = new ArrayCollection([{label:"根据查询结果"},{label:""}]);
[Bindable]
public var dat:ArrayCollection=new ArrayCollection();
private var resultDownUrl:String="";
private var filerefer:FileReference=new FileReference();
protected function basemodule1_creationCompleteHandler(event:FlexEvent):void
{
// TODO Auto-generated method stub
//获得文件到出路径
contcatExport.addEventListener(ResultEvent.RESULT,getExportUrl);
contcatExport.exportContactDirectory("","","");
//下载完成后删除服务器文件
filerefer.addEventListener(Event.COMPLETE, onDownloadComplete);
}
private function getExportUrl(evs:ResultEvent):void{
resultDownUrl=evs.result.toString();
}
protected function onDownloadComplete(e:Event):void {
var file:FileReference = e.target as FileReference;
Alert.show("文件下载成功! 其中文件名: " + file.name + " 文件大小:" + file.size);
/* statisDownload.exportDel(resultDownUrl); */
}
private function init():void{
}
protected function group1_clickHandler(event:MouseEvent):void
{
var downloadURL:URLRequest=new URLRequest();
downloadURL.url =resultDownUrl; //"report/201310161910736.zip";
filerefer.download(downloadURL);
}
protected function getDownloadURL(evs:ResultEvent):void{
var result:Object=JSON.parse(evs.result.toString());
}
protected function labelImport_clickHandler(event:MouseEvent):void
{
}
protected function labelCancel_clickHandler(event:MouseEvent):void
{
// TODO Auto-generated method stub
close_Cli();
}
public function close_Cli():void{
SysEvents.dispatch(SysEvents.REMOVE_ALL_MODULE,"");
}
]]>
</fx:Script>
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
<s:RemoteObject id="contcatExport" destination="contcatExport"/>
</fx:Declarations>
<s:Group width="100%" height="100%">
<s:Rect width="100%" height="100%">
<s:fill>
<s:SolidColor alpha="0.5" color="#000000"/>
</s:fill>
</s:Rect>
</s:Group>
<fx:Declarations>
<mx:HTTPService id="sevlet" resultFormat="e4x"/>
</fx:Declarations>
<s:HGroup width="100%" height="100%" horizontalAlign="center" verticalAlign="middle">
<commons:myPop clo="{close_Cli}" iconPath="assets/requirM/7_.png" labelStr="目录导出"
skinClass="Skins.myPopSkin">
<s:VGroup left="5" right="5" top="10" bottom="5" width="500" height="400" gap="10">
<s:HGroup width="100%" paddingLeft="20" verticalAlign="middle">
<mx:ComboBox id="items" x="10" y="34" width="228" dataProvider="{comboDatas}" selectedIndex="0"/>
</s:HGroup>
<s:HGroup width="100%" paddingLeft="20" verticalAlign="middle">
<s:Label width="40%" text="文件组织方式: "/>
<s:Label width="40%" text="大文件" color="red"/>
</s:HGroup>
<s:HGroup width="100%" gap="40" horizontalAlign="center" verticalAlign="middle">
<s:Group width="109" height="35" buttonMode="true"
click="group1_clickHandler(event)">
<s:Image source="assets/pop/25.png"/>
<s:HGroup width="109" height="35" horizontalAlign="center"
verticalAlign="middle">
<s:Label text="确定"/>
</s:HGroup>
</s:Group>
<s:Group width="109" height="35" buttonMode="true">
<s:Image source="assets/pop/25_.png"/>
<s:HGroup width="109" height="35" horizontalAlign="center"
verticalAlign="middle">
<s:Label text="取消" click="close_Cli()"/>
</s:HGroup>
</s:Group>
</s:HGroup>
</s:VGroup>
</commons:myPop>
</s:HGroup>
</utils:BaseModule>

浙公网安备 33010602011771号