0

i have a Flash application with a datagrid filled with filenames and i want the user to be able to check multiple files from that grid and download them to his local drive. The files are stored in a blob field in a database and im using php ro retrieve them. Im trying to use URLLoader and FileReference to download the files, but it is not working. Here's the code:

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);

        }

Can anyone help me on this?

  • What exactly is not working? (i.e. what's happening?) – Corey Adler Mar 11 '13 at 15:02
  • 1
    I haven't looked into it in detail, but I don't think you can do it that way. You are not supposed to use a URLLoader, I believe, instead you should use the FileReference.dowload() method. Also, there are security restrictions involved, where user interaction is required, you can't download files with script alone, without user interaction (unless you are building an AIR app). See http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/FileReference.html#download() – Lars Blåsjö Mar 11 '13 at 15:29
  • thank you Lars Blåsjö. i tried with Filereference and now i can download the first file in the list. the others throw an exception. How can i make it work for more than one file? – Aparicio JJ Mar 13 '13 at 17:44

1 Answers1

0

Using FileReference.download instead of URLLoader, i can download the first file selected in the datagrid, but the others throw exception Error#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();
            }   
        }