2013-03-11 135 views
0

我有一个Flash格式的应用程序与数据网格填充文件名,我希望用户能够检查该网格的多个文件,并将它们下载到他的本地驱动器。这些文件存储在数据库的blob字段中,并使用php ro检索它们。我试图使用URLLoader和FileReference下载文件,但它不工作。 下面的代码:下载多个文件闪光灯

private function downloadSelected():void { 
      var dp:Object=dg.dataProvider; 
      var cursor:IViewCursor=dp.createCursor(); 

      while(!cursor.afterLast) 
      { 

       //Alert.show(cursor.current.IsChecked.toString()); 
       if (cursor.current.IsChecked.toString()=="true") { 
        //myAmostras.push(cursor.current.CodBarras.toString()); 
        //nenhumaAmostra=false; 

       var u:URLRequest= new URLRequest; 
        var variables:URLVariables = new URLVariables();    
        variables.amostras = cursor.current.CodBarras.toString(); 
        variables.disposition="attach"; 
        u = new URLRequest("savereports.php"); 
        u.method = URLRequestMethod.POST; 
        u.data=variables; 


        pdfloader = new URLLoader(); 
        //pdfloader.dataFormat=URLLoaderDataFormat.BINARY; 
        pdfloader.addEventListener(Event.COMPLETE, completeHandler,false,0,true); 
        pdfloader.addEventListener(IOErrorEvent.IO_ERROR, downloadError); 


        try { 
         pdfloader.load(u); 
        } catch (error:Error) { 
         Alert.show("Unable to load requested document."); 
        } 

       } 
       cursor.moveNext(); 
      } 
     } 

     private function downloadError(event:Event):void { 
      Alert.show("Error loading file"); 
     } 



     private function completeHandler(event:Event):void { 

      var myFileReference:FileReference = new FileReference(); 

      myFileReference.save(event.target.data); 

     } 

谁能帮助我在这?

+0

恰好是不工作怎么办? (即发生了什么?) – IronMan84 2013-03-11 15:02:47

+1

我没有详细研究它,但我认为你不能这样做。我相信你不应该使用URLLoader,而应该使用FileReference.dowload()方法。此外,还存在安全限制,需要用户交互,您无法单独使用脚本下载文件,也无需用户交互(除非您正在构建AIR应用程序)。请参阅http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/FileReference.html#download() – 2013-03-11 15:29:28

+0

谢谢LarsBlåsjö。我试着用Filereference,现在我可以下载列表中的第一个文件。其他人抛出异常。我怎样才能让它适用于多个文件? – 2013-03-13 17:44:50

回答

0

使用替代的URLLoader FileReference.download,我可以下载在DataGrid中选择的第一个文件,但其他人抛出异常错误#2041

 private function downloadSelected():void { 
      var dp:Object=dg.dataProvider; 
      var cursor:IViewCursor=dp.createCursor(); 

      var i:int=0; 
      while(!cursor.afterLast) 
      { 
       if (cursor.current.IsChecked.toString()=="true") { 

        var u:URLRequest= new URLRequest; 
        var variables:URLVariables = new URLVariables();    
        variables.amostras = cursor.current.CodBarras.toString(); 
        variables.disposition="attach"; 
        u = new URLRequest("savereports.php"); 
        u.method = URLRequestMethod.POST; 
        u.data=variables; 

        try { 


         var myFileReference:FileReference = new FileReference(); 
         myFileReference.download(u,cursor.current.CodBarras.toString()+".pdf"); 
        } catch (error:Error) { 
         Alert.show("Unable to load " + cursor.current.CodBarras.toString()+".pdf"); 
        } 

       } 
       cursor.moveNext(); 
      } 
     }