2009-12-17 60 views
-2

有人可以幫助我嗎?多次下載flex

我想從服務器端下載文件到客戶端,而不會在服務器端發生任何更新時提示用戶下載窗口。

現在我正在使用urlstream類,但第一個文件正在完全下載文件內容的其餘部分下載。從其他崗位採取

編輯

代碼示例。警告:這是一個huuuge塊o'code。

<?xml version="1.0" encoding="utf-8"?> 
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" 
    xmlns:MyComp="client.components.*" layout="absolute" height="554" width="817" 
    backgroundGradientAlphas="[1.0, 0.0]" 
    backgroundGradientColors="[#ABA9A7, #03143B]" creationComplete="init();"> 

<mx:Script> 
<![CDATA[ 
    import flash.system.Capabilities; 
    import mx.collections.XMLListCollection; 
    import mx.rpc.Fault; 
    import mx.rpc.events.ResultEvent; 
    import mx.rpc.events.FaultEvent; 
    import mx.events.ListEvent; 
    import mx.collections.ArrayCollection; 
    import flash.data.SQLConnection; 
    import flash.errors.SQLError; 
    import flash.events.SQLErrorEvent; 
    import flash.events.SQLEvent; 
    import flash.filesystem.File; 
    import flash.errors.SQLError; 
    import mx.controls.Alert; 
    import mx.events.CloseEvent; 
    import flash.net.*; 
    import flash.filesystem.*; 
    import flash.events.OutputProgressEvent; 

    private var urlstring:String="server path"; 
    private var urlReq:URLRequest; 
    private var urlStream:URLStream; 
    private var fileData:ByteArray = new ByteArray(); 
    private var sqlConnection:SQLConnection =null; 
    private var sql:String; 
    private var result:SQLResult; 
    private var stmt:SQLStatement=null; 
    private var catid:int=0; 
    private var testcollection:Array=new Array(); 

    [Bindable] 
    private var DGArray:ArrayCollection = new ArrayCollection(testcollection); 
    private var number:int; 
    private var selection:Boolean=false; 
    private var timestamp:String; 
    private var xmlcol:XMLListCollection; 
    private var categoryid:int=0; 
    private var numbers:int; 
    private var index:int=0; 
    private var findex:int=0; 
    private var nupdates:int=0; 
    private var newfile:String=""; 
    private var selectstream:int=1; 
    private var startdownload:Boolean=false; 

    public function init():void{ 
     userRequest.send(null); 
     sqlConnection= new SQLConnection(); 
     //create a new db-file in the application storage location 
     var file:File = new File("app:/E-catalog.db"); 
     sqlConnection.addEventListener(SQLEvent.OPEN,dbOpenedHandler); 
     sqlConnection.addEventListener(SQLErrorEvent.ERROR,dbErrorHandler); 

     sqlConnection.open(file); 

     getData(); 
    } 

    public function loaded(event:Event):void { 
     urlStream.readBytes(fileData, 0, urlStream.bytesAvailable); 

     for(var x:int=0;x<nupdates;x++){ 
      var filename:Object=xmlcollection.getItemAt(x); 
      newfile=filename['pdfimage']; 
      writeAirFile(); 
     } 

    } 

    public function writeAirFile():void { 
     /* 
     var obj:Object=new Object; 
     startdownload=true; 
     var file:File = File.applicationStorageDirectory.resolvePath(newfile); 
     fileStream = new FileStream(); 

     fileStream.openAsync(file, FileMode.WRITE); 
     fileStream.writeBytes(fileData, 0, fileData.length); 
     fileStream.addEventListener(Event.COMPLETE,progressHandler,true,10,true); 

     for(var x:int=0;x<fileData.length;x++){} 

     startdownload=false; 
     fileStream.close(); 
     */ 
    } 

    public function progressHandler(event:Event){ 
     Alert.show("File downloading"); 
    } 

    public function dbOpenedHandler(event:SQLEvent):void{} 

    public function dbErrorHandler(error:SQLError):void{} 

    public function getData():void{ 
     sql = "SELECT STRFTIME('%Y-%m-%d %H:%M:%S',lupdated_dt)as lupdated_dt FROM update_mas where delflag=0 "; 
     stmt = new SQLStatement(); 
     stmt.sqlConnection = sqlConnection; 

     stmt.text = sql; 

     stmt.addEventListener(SQLEvent.RESULT, selectResult); 
     stmt.addEventListener(SQLErrorEvent.ERROR, selectError); 
     stmt.execute(); 
    } 

    public function selectResult(event:SQLEvent){ 
     DGArray.removeAll(); 
     number=1; 
     var result = stmt.getResult(); 
     var numRows = result.data.length; 

     for (var i = 0; i < numRows; i++){ 
      for(var col:String in result.data[i]){ 
       timestamp=result.data[i][col]; 
      } 
     } 

     updateRequest.url="serverpath ?time="+timestamp+""; 
     updateRequest.send(null); 
    } 

    public function selectError(event):void{ 
     stmt.removeEventListener(SQLEvent.RESULT, selectResult); 
     stmt.removeEventListener(SQLErrorEvent.ERROR, selectError); 
     Alert.show("SELECT error:"); 
    } 

    public function displayPOPUP(event:ResultEvent):void{ 
     nupdates=((int)(event.result)); 
     if(nupdates==0){ 
     }else{ 
      selection=Alert.show(""+nupdates+"Updates available", "UPDATES", Alert.YES|Alert.NO, this, alertClickHandler); 
     } 
    } 

    private function alertClickHandler(event:CloseEvent):void { 
     if (event.detail==Alert.YES){ 
      downloadRequest.url="serverpath ?time="+timestamp; 
      downloadRequest.send(null); 
     }else 
      Alert.show("download cancelled"); 
    } 

    public function displayResult(event:ResultEvent):void{ 
     var record:String=""; 
     sql=""; 
     for(index=0;index<xmlcollection.length;index++){ 
      var category:Object=xmlcollection.getItemAt(index); 
      sql="select Id FROM CATEGORY where delflag=0 and cat_name='"+category['catname']+"'"; 
      stmt = new SQLStatement(); 
      stmt.sqlConnection = sqlConnection; 
      stmt.text = sql; 
      stmt.addEventListener(SQLEvent.RESULT,checkRecord); 
      stmt.addEventListener(SQLErrorEvent.ERROR,checkError); 
      stmt.execute(); 
     } 
    } 

    public function checkRecord(event:Event):void{ 
     var category:Object=xmlcollection.getItemAt(index); 
     var path:String=""; 
     var result:SQLResult = stmt.getResult(); 
     if(result.data==null){ 
      var sql:String= "INSERT INTO CATEGORY (cat_name, created_user,created_dt,updated_user,updated_dt,image_jpg,image_pdf) "; 
      sql += "VALUES ('"+category['catname']+"',"; 
      sql +="'admin','"+category['cdate']+"','admin','"+category['udate']+"'"+category['pictimage']+"','"+category['pdfimage']+"')"; 
      stmt = new SQLStatement(); 
      stmt.sqlConnection = sqlConnection; 
      stmt.text = sql; 
      stmt.addEventListener(SQLEvent.RESULT,insertRecord); 
      stmt.addEventListener(SQLErrorEvent.ERROR,insertError); 

      stmt.execute(); 
     }else{ 
      sql=""; 
      sql="update CATEGORY set created_dt='"+category['cdate']+"', updated_dt='"+ category['udate']+"',image_jpg='"+category['pictimage']+"', image_pdf='"+category['pdfimage']+"' where delflag=0 and cat_name='"+category['catname']+"'"; 
      stmt = new SQLStatement(); 
      stmt.sqlConnection = sqlConnection; 
      stmt.text = sql; 
      stmt.addEventListener(SQLEvent.RESULT,updateResult); 
      stmt.addEventListener(SQLErrorEvent.ERROR,updateError); 
      stmt.execute(); 
     } 

     userRequest.send(null); 

     var ludt_dt:Object=xmlcollection.getItemAt((xmlcollection.length-1)); 
     timestamp=""; 
     timestamp=ludt_dt['udate']; 
     sql=""; 
     sql="update update_mas set lupdated_dt='"+timestamp+"' where delflag=0 "; 
     stmt = new SQLStatement(); 
     stmt.sqlConnection = sqlConnection; 
     stmt.text = sql; 
     stmt.addEventListener(SQLEvent.RESULT,updateResult); 
     stmt.addEventListener(SQLErrorEvent.ERROR,updateError); 
     stmt.execute(); 
     var temp:int=0; 
     for(var x:int=0;x<=xmlcollection.length-1;){ 
      var urlstring:String="http://sidssoldc:81/lessons/ravi/"; 
      var url:Object=xmlcollection.getItemAt(x); 
      urlstring+=url['pdfimage']; 
      path=url['pdfimage']; 
      while((path.indexOf('/',0)!=-1)){ 
       path=path.slice(path.indexOf('/',0)+1,path.length); 
      } 

      urlReq=new URLRequest(urlstring); 
      var filename:Object=xmlcollection.getItemAt(x); 
      var loader:URLLoader=new URLLoader(); 
      loader.dataFormat="binary"; 

      selectstream=2; 
      loader.load(urlReq); 

      loader.addEventListener(Event.COMPLETE,function(event:Event){ 
       var loader:URLLoader=(URLLoader)(event.target); 
       loader.dataFormat="binary"; 
       var ofstream2:FileStream= new FileStream(); 
       var ofstream1:FileStream= new FileStream(); 
       var ofstream3:FileStream= new FileStream(); 
       if(selectstream==1){ 
        var ofile1:File = File.applicationStorageDirectory.resolvePath('images/pdf/'+path); 
        ofstream1.openAsync(ofile1, FileMode.WRITE); 
        ofstream1.writeBytes(loader.data, 0, loader.bytesTotal); 
        selectstream++; 
       }else if(selectstream==2){ 
        ofstream1.close(); 
        var ofile2:File = File.applicationStorageDirectory.resolvePath('images/pdf/'+path); 
        ofstream2.openAsync(ofile2, FileMode.WRITE); 
        ofstream2.writeBytes(loader.data, 0, loader.bytesTotal); 
        selectstream++; 
       }else if(selectstream==3){ 
        ofstream2.close(); 
        var ofile3:File = File.applicationStorageDirectory.resolvePath('images/pdf/'+path); 
        ofstream3.openAsync(ofile3, FileMode.WRITE); 
        ofstream3.writeBytes(loader.data, 0, loader.bytesTotal); 
       } 

       if(selectstream==3){ 
        ofstream3.close(); 
       } 
      }); 

      x++; 

      temp=loader.bytesTotal; 

      checkStream.close(); 
     } 

     /*urlStream=new URLStream(); 
     urlStream.addEventListener(Event.COMPLETE, function(event:Event){ 

     urlStream.readBytes(fileData, 0, urlStream.bytesAvailable); 

     for(var x:int=0;x<nupdates;x++){ 
      var filename:Object=xmlcollection.getItemAt(x); 
      newfile=filename['pdfimage']; 
      var obj:Object=new Object; 
      startdownload=true; 
      var file:File = File.applicationStorageDirectory.resolvePath(newfile); 
      var fileStream:FileStream = new FileStream(); 

      fileStream.openAsync(file, FileMode.WRITE); 
      fileStream.writeBytes(fileData, 0, fileData.length); 
      fileStream.addEventListener(Event.COMPLETE,progressHandler,true,10,true); 

      for(var x:int=0;x<fileData.length;x++){ 
       startdownload=false; 
       fileStream.close(); 
      } 
     }); 

     urlStream.load(urlReq); 
     x++;*/ 
    } 

    public function insertRecord(event:Event):void{} 

    public function insertError(event:Event):void{} 

    public function updateResult(event:Event):void{} 

    public function updateError(event:Event):void{ 
     Alert.show("update failed"); 
    } 

    public function checkError(event:Event):void{ 
     Alert.show("error"); 
    } 
    public function showError(event:FaultEvent):void{ 
     Alert.show(""+event.fault); 
    } 

    public function itemClick(event:ListEvent):void{} 
]]> 
</mx:Script> 

<mx:DataGrid id="dgUserRequest" sortableColumns="false" x="99" y="115" width="364" textAlign="left" itemClick="itemClick(event);" rowHeight="30" doubleClickEnabled="false" height="297" themeColor="#ACF4F8" visible="true" editable="false" dataProvider="{catxmlcollection}" color="#000C0E" fontSize="11" > 
    <mx:columns> 
     <mx:DataGridColumn id="ID" headerText="Id" visible="false" dataField="catid" /> 
     <mx:DataGridColumn id="snumber" width="70" headerText="SerialNo" dataField="sno" visible="true" /> 
     <mx:DataGridColumn id="CATEGORY3" width="250" headerText="CATEGORY" dataField="catname" visible="true" /> 
    </mx:columns> 
</mx:DataGrid> 

<mx:XMLListCollection id="catxmlcollection" source="{userRequest.lastResult.categories.category}"/> 

<mx:HTTPService id="userRequest" url="http://192.168.10:81/lessons/ravi/cat.php" fault="showError(event);" useProxy="false" method="GET" resultFormat="e4x" /> 
<mx:HTTPService id="updateRequest" result="displayPOPUP(event);" fault="showError(event);" method="GET" > </mx:HTTPService> 

<mx:XMLListCollection id="xmlcollection" source="{downloadRequest.lastResult.Categories.Category}" /> 

<mx:HTTPService id="downloadRequest" result="displayResult(event);" fault="showError(event);" useProxy="false" method="GET" resultFormat="e4x" ></mx:HTTPService> 
<mx:HTTPService id="categoryRequest"></mx:HTTPService> 

<mx:Label x="216" y="53" text="Category Master" fontWeight="bold" fontSize="12" width="151"/> 

</mx:WindowedApplication> 
+1

Ouch。我的耳朵。請停止大聲呼喊。 – 2009-12-17 04:44:55

+0

我們需要一些源代碼來幫助解決代碼中的問題... – Aaron 2009-12-17 04:56:47

+0

urlReq = new URLRequest(urlstring); var filename:Object = xmlcollection.getItemAt(x); var loader:URLLoader = new URLLoader(); loader.dataFormat =「binary」; loader.load(urlReq); loader.addEventListener(Event.COMPLETE,function(event:Event) {var loader:URLLoader =(URLLoader)(event.target); loader.dataFormat =「binary」; var ofstream2:FileStream = new FileStream() ; VAR ofstream1:的FileStream =新的FileStream(); VAR ofstream3:的FileStream =新的FileStream();如果 (selectstream == 1) { VAR ofile1:文件= File.applicationStorageDirectory 與resolvePath('圖片/ PDF /'+ path); ofstream1.openAsync(ofile1,FileMode.WRITE); – 2009-12-17 11:05:12

回答

0

您需要提供更多詳細信息。我甚至無法理解您是要刷新服務器中的數據還是希望在未經用戶許可的情況下下載文件。如果是前者,那麼投票就是要走的路。對於後者,您不妨關閉商店,因爲沒有人會使用不需要先詢問就下載內容的應用程序。

.P

+0

它可能是一個「自動更新」類型的提示...或者如果你喜歡「設置它並忘記它!」自動下載文件是有正當理由的,但是你確實沒有人會使用(或者應該使用)下載內容而不先詢問的應用程序。它應該是應用程序配置中的用戶應用設置。 – 2009-12-17 15:42:20

0

同時發送多個請求時,我有一些問題......時,例如,試圖在只有一些被正確接收,同時裝載20個圖像。我不確定是什麼導致了這一點,但我建議你嘗試按順序進行轉移,因爲它解決了我所有的問題。在實踐中,你可能會把它們放在一個數組中,並且當前一個完成時總是取出並移除它。