下载多个文件flash

时间:2013-03-11 14:43:13

标签: flash flex filereference urlloader

我有一个带有文件名的数据网格的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);

        }

有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

使用FileReference.download而不是URLLoader,我可以下载在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();
            }   
        }