前幾天寫了一篇jsp頁面利用ajaxFileUpload上傳文件,現在把flex上傳頁面也分享出來:
前臺頁面
<?xml version="1.0" encoding="utf-8"?>
<s:HGroup xmlns:fx="<a target=_blank href="http://ns.adobe.com/mxml/2009">http://ns.adobe.com/mxml/2009</a>"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="hgroup1_creationCompleteHandler(event)"
width="100%" height="30" >
<fx:Script>
<![CDATA[
import mx.collections.ArrayList;
import mx.controls.Alert;
import mx.events.FlexEvent;
private var file:FileReference = new FileReference;
public var fileList:ArrayList;
public var manualCheck:ManualCheck;
protected function hgroup1_creationCompleteHandler(event:FlexEvent):void
{
// TODO Auto-generated method stub
// file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,fileUploadCompleteHandler);
file.addEventListener(Event.SELECT, fileSelect);
// file.addEventListener(IOErrorEvent.IO_ERROR,uploadError);
}
protected function button2_clickHandler(event:MouseEvent):void
{
// 瀏覽
file.browse();
}
private function fileSelect(e:Event):void
{
if(fileList.getItemIndex(file) == -1){
fileList.addItem(file);
}
fileName.text = file.name;
}
// private function fileUploadCompleteHandler(e:DataEvent):void{
//
// }
private function uploadError(e:IOErrorEvent):void{
this.cursorManager.removeBusyCursor();
//獲取後臺的錯誤提示信息
Alert.show("上傳出錯。","提示");
}
protected function button3_clickHandler(event:MouseEvent):void
{
// 刪除
fileList.removeItem(this.file);
manualCheck.group.removeElement(this);
}
]]>
</fx:Script>
<fx:Declarations>
<!-- 將非可視元素(例如服務、值對象)放在此處 -->
</fx:Declarations>
<s:TextInput id="fileName" width="350"/>
<mx:Button label="瀏覽" click="button2_clickHandler(event)" fontWeight="bold"
overSkin="@Embed(source='/assets/dfpBtn/btnliulan2.png')"
skin="@Embed(source='/assets/dfpBtn/btnliulan.png')"/>
<mx:Button click="button3_clickHandler(event)"
overSkin="@Embed(source='/assets/dfpBtn/deletebtn2.png')"
skin="@Embed(source='/assets/dfpBtn/deletebtn.png')"/>
</s:HGroup>
as:
var file:FileReference = fileList.getItemAt(i) as FileReference;
var request:URLRequest=new URLRequest("s/upload/uploadFile");
request.data=new URLVariables();
request.data.orderRedoRecord=n;
try{
file.upload(request,"file");
} catch (error:Error){
isSuccess = false;
Alert.show("文件上傳失敗");
}
後臺java:
@RequestMapping(value = "/uploadFile")
@ResponseBody()
public String UploadFiles(@RequestParam(value = "file") MultipartFile file) {
String result = "";
if (!file.isEmpty()) {
try {
String attachName = file.getOriginalFilename();
logger.info(attachName);
String filePath = "你的路徑";
//此處省去業務代碼
//FileUtils.saveDataToFile(file.getBytes(), filePath);
result = "上傳成功";
} catch (Exception e) {
// TODO Auto-generated catch block
result = "上傳失敗";
e.printStackTrace();
}
}
return result;
}