之前會了單個上傳圖片,然後自己想搞一下一次上傳多張圖片~~
文檔類Files.as:
package {<br />import flash.display.Sprite;<br />import flash.events.Event;<br />import flash.events.MouseEvent;<br />import flash.net.FileFilter;<br />public class Files extends Sprite {//檔案類<br />private var container:Sprite=new Sprite();//容器存放UploadFile<br />private var fileNums:int = 0;//現有多少檔案<br />private const fileY:int = 50;//UploadFile檔案的y座標<br />private const fileYGap:int = 50;//UploadFile檔案的y座標間距<br />private var fileUploadSuccess:int = 0;//上傳成功的檔案數<br />private var fileUploadFail:int = 0;//上傳失敗的檔案數<br />private var url:String = "http://localhost:8080/upload2/index.jsp";//目標地址<br />private var myFileFilter:FileFilter = new FileFilter("Images(*.jpg;*.gif;*.png)","*.jpg;*.gif;*.png");//檔案過濾<br />public function Files () {<br />init ();//初始化<br />addUploadFile ();//添加一個UploadFile<br />}<br />private function init ():void {<br />addChild (container);//添加容器<br />upload_btn.addEventListener (MouseEvent.CLICK,onUpload);//上傳按鈕添加事件<br />allDelete_btn.addEventListener (MouseEvent.CLICK,onAllDelete);//全部刪除按鈕添加事件<br />this.addEventListener (UploadFile.FILE_READY,onReady);//偵聽檔案準備事件<br />this.addEventListener (UploadFile.FILE_DELETE,onDelete);//偵聽檔案刪除事件<br />}<br />private function onUpload (event:MouseEvent):void {//上傳<br />if (container.numChildren == 1)//如果只有一個UploadFile,也就是還沒選擇任何檔案<br />{<br />message.text = "你沒有選擇任何檔案";<br />return;<br />}<br />for (var i:int=0; i<container.numChildren; i++)<br />{<br />(container.getChildAt(i) as UploadFile).uploadMyFile();//逐一上傳<br />}<br />this.addEventListener (UploadFile.FILE_UPLOAD_SUCCESS,onFileUploadSuccess);//偵聽成功的事件<br />this.addEventListener (UploadFile.FILE_UPLOAD_FAIL,onFileUploadFail);//偵聽失敗的事件</p><p>}<br />private function onFileUploadSuccess (event:Event):void {//處理上傳成功的事件<br />fileUploadSuccess++;//成功數++<br />message.text = "成功上傳" + fileUploadSuccess + "個檔案!" + "上傳失敗" + fileUploadFail + "個檔案!";//更新資訊列<br />if (fileUploadSuccess + fileUploadFail == fileNums)//如果成功數+失敗數是檔案數,則全部已經調用成功<br />{<br />fileUploadSuccess = 0;<br />fileUploadFail = 0;<br />deleteAllFiles ();//刪除所有檔案<br />}<br />}<br />private function onFileUploadFail (event:Event):void {//處理上傳失敗的事件<br />fileUploadFail++;//失敗數++<br />message.text = "成功上傳" + fileUploadSuccess + "個檔案!" + "上傳失敗" + fileUploadFail + "個檔案!";//更新資訊列<br />if (fileUploadSuccess + fileUploadFail == fileNums)//如果成功數+失敗數是檔案數,則全部已經調用成功<br />{<br />fileUploadSuccess = 0;<br />fileUploadFail = 0;<br />deleteAllFiles ();//刪除所有檔案<br />}<br />}<br />private function onAllDelete (event:MouseEvent):void {//刪除所有檔案<br />if (container.numChildren == 1)//如果只有一個UploadFile,也就是還沒選擇任何檔案<br />{<br />message.text = "你沒有選擇任何檔案";<br />return;<br />} else<br />{<br />message.text = "成功刪除所有檔案";<br />}<br />for (var i:int=0; container.numChildren!=1; )<br />{<br />container.removeChildAt (i);//移除所有除了最後一個UploadFile<br />}<br />arrangeFiles ();//重新排列container<br />fileNums = 0;//檔案數為0<br />}<br />private function deleteAllFiles ():void {<br />for (var i:int=0; container.numChildren!=1; )<br />{<br />container.removeChildAt (i);//移除所有除了最後一個UploadFile<br />}<br />arrangeFiles ();//重新排列container<br />fileNums = 0;//檔案數為0<br />}<br />private function onReady (event:Event):void {//處理檔案準備的事件<br />fileNums++;//檔案數++<br />addUploadFile ();//添加一個UploadFile<br />}<br />private function onDelete (event:Event):void {<br />fileNums--;//檔案數--<br />container.removeChild (event.target as Sprite);//刪除發出事件的那個UploadFile<br />arrangeFiles ();//重新排列container<br />}<br />private function arrangeFiles ():void {//排列container<br />for (var i:int=0; i<container.numChildren; i++)<br />{<br />(container.getChildAt(i) as Sprite).y=fileY+i*fileYGap;//排列<br />}<br />}<br />private function addUploadFile ():void {//添加一個UploadFile<br />var tempFile:UploadFile = new UploadFile(url,myFileFilter);//設定目標地址和檔案過濾<br />container.addChild (tempFile);//container添加<br />tempFile.y = fileY + fileNums * fileYGap;<br />}<br />}<br />}
UploadFile.as:
package {<br />import flash.display.Sprite;<br />import flash.net.FileReference;<br />import flash.net.FileFilter;<br />import flash.net.URLRequest;<br />import flash.events.Event;<br />import flash.events.MouseEvent;<br />import flash.events.IOErrorEvent;<br />public class UploadFile extends Sprite {//單個檔案上傳類<br />private var myFile:FileReference;//檔案擷取<br />private var fileIsReady:Boolean = false;//檔案是否準備好<br />private var url:String;//上傳目標地址<br />private var myFileFilter:FileFilter;//過濾的檔案<br />private var urlRequest:URLRequest;//目標請求<br />public static var FILE_READY:String = "file_ready";//準備事件<br />public static var FILE_DELETE:String = "file_delete";//刪除事件<br />public static var FILE_UPLOAD_SUCCESS:String = "file_upload_success";//上傳成功事件<br />public static var FILE_UPLOAD_FAIL:String = "file_upload_fail";//上傳失敗事件<br />public function UploadFile (url:String,myFileFilter:FileFilter) {//建構函式<br />this.url = url;//儲存地址<br />this.urlRequest = new URLRequest(url);//儲存請求<br />this.myFileFilter = myFileFilter;//設定過濾檔案<br />init ();//初始化<br />}<br />private function init ():void {<br />myFile=new FileReference();<br />delete_btn.visible = false;//刪除按鈕不可見<br />browse_btn.addEventListener (MouseEvent.CLICK,onBrowse);//瀏覽按鈕添加事件<br />}<br />private function onBrowse (event:MouseEvent):void {<br />myFile.browse ([myFileFilter]);//瀏覽<br />myFile.addEventListener (Event.SELECT,onSelect);//有檔案被選擇時<br />}<br />private function onSelect (event:Event):void {<br />fileName_txt.text = event.target.name;//更新常值內容<br />delete_btn.visible = true;//刪除按鈕可見<br />if (fileIsReady == false)//如果沒有準備好,就派發準備好的事件<br />{<br />this.dispatchEvent (new Event(FILE_READY,true));<br />}<br />fileIsReady = true;//準備好<br />delete_btn.addEventListener (MouseEvent.CLICK,onDelete);//刪除按鈕添加事件<br />}<br />private function onDelete (event:MouseEvent):void {<br />delete_btn.removeEventListener (MouseEvent.CLICK,onDelete);<br />this.dispatchEvent (new Event(FILE_DELETE,true));//派發被刪除的事件<br />delete_btn.visible = false;//刪除按鈕不可見<br />fileIsReady = false;//檔案設為未準備<br />fileName_txt.text = "";//更新常值內容<br />myFile.cancel ();//檔案取消<br />}<br />public function uploadMyFile ():void {//上傳介面<br />if (fileIsReady)//如果已經準備好<br />{<br />myFile.upload (urlRequest,myFile.name);//上傳<br />myFile.addEventListener (Event.COMPLETE,onComplete);//添加完成事件<br />myFile.addEventListener (IOErrorEvent.IO_ERROR,onError);//添加失敗事件<br />}<br />}<br />public function deleteMyFile ():void {//刪除介面<br />delete_btn.removeEventListener (MouseEvent.CLICK,onDelete);<br />this.dispatchEvent (new Event(FILE_DELETE,true));<br />delete_btn.visible = false;//刪除按鈕不可見<br />fileIsReady = false;//檔案設為未準備<br />fileName_txt.text = "";//更新常值內容<br />myFile.cancel ();//檔案取消<br />}<br />private function onComplete (event:Event):void {//上傳完成<br />delete_btn.visible = false;//刪除按鈕不可見<br />fileIsReady = false;//檔案設為未準備<br />myFile.cancel ();//檔案取消<br />fileName_txt.text = "上傳成功!";//更新常值內容<br />this.dispatchEvent (new Event(FILE_UPLOAD_SUCCESS,true));//派發上傳成功事件<br />}<br />private function onError (event:IOErrorEvent):void {//上傳失敗<br />delete_btn.visible = false;//刪除按鈕不可見<br />fileIsReady = false;//檔案設為未準備<br />myFile.cancel ();//檔案取消<br />fileName_txt.text = "上傳失敗!";//更新常值內容<br />this.dispatchEvent (new Event(FILE_UPLOAD_FAIL,true));//派發上傳失敗事件<br />}<br />}<br />}
其中文檔類的upload_btn和allDelete_btn和message是Main.fla裡面的元件
:http://www.fileupyours.com/files/262979/upload.rar
這裡是用戶端的代碼
其他JSP和JavaBean的部分大家可以參考:
http://hi.baidu.com/%D3%EA%BA%F3%B5%C4%B5%AD%C0%B6%C7%E7%BF%D5/blog/item/f888b9839255f4a90df4d2cf.html
http://hi.baidu.com/%D3%EA%BA%F3%B5%C4%B5%AD%C0%B6%C7%E7%BF%D5/blog/item/604606ccee6dba5a0eb345c9.html
這個老兄很厲害喔~~呵呵~小弟菜啊~~