23 Replies Latest reply on Dec 10, 2010 1:43 AM by marioterzo

    Read a CSV file into Flex

    Kieran Briggs Level 1
      Hi all,

      I'm new to flex and have been asked to provide a small widget that will display the contents of a csv in a list. Can I use the HTTPService to open a csv file and then create an actionscript 3 function to parse it? If so can anyone point me to a tutorial on how to parse a csv file please.

      Thanks in advance
        • 1. Re: Read a CSV file into Flex
          ntsiii Level 3
          I've not done this in AS, but it shouldn't be too difficult.

          You will just need to use split() to first put the lines in an array then again to put the fields in an array for each line.

          Split the fields on comma, obviously. I'd have to experiment some to see how to split the lines on CR.

          You might google this, someone has probably already done it.

          Tracy
          • 2. Re: Read a CSV file into Flex
            parrygill Level 1

            Hello Sir,

             

              I am new to flex and want to read a csv file and using below code but does not seems to be working. Can you please help?

              i use below code but did not work

             

            var file:File = evt.currentTarget as File;
                     file = file.resolvePath(file.nativePath);
                     var fileStream:FileStream = new FileStream();
                     fileStream.open(file, FileMode.READ);     
                     var fileData:String =  fileStream.readUTFBytes(fileStream.bytesAvailable);       
                     var endings:Array = [File.lineEnding, "\n", "\r"];

             

            but for some reason it return "ÐÏ ࡱá" funny value. Any idea why don't i get the correct data from file.

             

            belwo is the csv file i am trying to open.

            Title

            Given_Name

            Surname

            Gong

            Salutation

            Position

            Organisation

            Address_Line_1

            Address_Line_2

            Address_Line_3

            Suburb

            State

            Postcode

            Country

            Home_Phone

            Fax

            Other_Phone

            User_Field_1

            User_Field_2

            User_Field_3

            User_Field_4

            Mobile_Phone

            Second_Address

            Second_Address_Line_1

            Second_Address_Line_2

            Second_Address_Line_3

            Second_Suburb

            Second_State

            Second_Country

            Second_Postcode

            Langcode

            Website

            Mr.

            Jeff

            Alexander

            Retention Marketing

            Monday, April 13th, 2009

            Mr.

            Anthony

            Demaso

            Retention Marketing

            Monday, April 13th, 2009

            Sally

            Swinamer

            Yield

            Monday, April 13th, 2009

            Chris

            Torbay

            Yield

            Monday, April 13th, 2009

            Annette

            Warring

            Genesis Vizeum

            Monday, April 13th, 2009

            Mr.

            Mark

            Khoury

            Genesis Vizeum

            Monday, April 13th, 2009

            Mr.

            Andy

            Thorndyke

            Thorsons

            Monday, April 13th, 2009

            Shannon

            Rutherford

            Central Reproductions

            Monday, April 13th, 2009

            Mr.

            Rob

            Greenwood

            Central Reproductions

            Monday, April 13th, 2009

            Lisa

            Marchese

            Des Rosiers

            Monday, April 13th, 2009

            Mr.

            Michael

            Whitcombe

            McMillan LLP

            Monday, April 13th, 2009

             

             

             

             

            Thanks,

            Gill

             

             

             

            • 3. Re: Read a CSV file into Flex
              *Prashant Shelke* Level 4

              Please find attachment.

              • 4. Re: Read a CSV file into Flex
                parrygill Level 1

                Thanks for your help.I have browse button to open the file and use will select the file location. That's why i am using current approach. can you please send me sample code to read the attached xls file please.

                • 5. Re: Read a CSV file into Flex
                  Chandra2034 Level 1

                   

                   

                  I am using following code to read xlsx file in Flex AIR 4 code. It not reading anything I am getting "PK   "  as output.

                  Please help me to read correctrly xlsx file in Flex.

                   

                  private function btnCSV_click():void

                  {

                   

                   

                  var strFilePath :String = File.applicationDirectory.nativePath + "\\Test.xlsx";

                   

                  var loader:URLLoader = new URLLoader();

                  configureListeners(loader);

                   

                   

                  var request:URLRequest = new URLRequest(strFilePath);

                  request.contentType =

                  "text/csv";

                   

                  try {

                  loader.load(request);

                  }

                  catch (error:Error) {

                   

                  trace ("Unable to load requested document.");

                  }

                   

                  }

                   

                  private function configureListeners(dispatcher:IEventDispatcher):void

                  {

                  dispatcher.addEventListener(Event.COMPLETE, completeHandlerCSV);

                  }

                   

                   

                  private function completeHandlerCSV(event:Event) :void

                  {

                   

                   

                  trace ( event.target.data );

                  }

                  • 6. Re: Read a CSV file into Flex
                    Flex harUI Adobe Employee

                    An xlsx file is not a CSV file.

                    • 7. Re: Read a XLSX file into Flex
                      Chandra2034 Level 1

                      Yeah, I can read CSV file but not xlsx file. How to read xlsx file ?

                      • 8. Re: Read a CSV file into Flex
                        Flex harUI Adobe Employee

                        You will need to find doc on the format and use ByteArray

                        • 9. Re: Read a xlsx file into Flex
                          Chandra2034 Level 1

                          Thanks Flex harUI. Please refer me any example which shows how to convert ByteArray to actual data. I read xlsx file using ByteArray format but could not decode result to actual data.

                           

                          Question : Is Flex AIR4 support reading xlsx file ?

                          • 10. Re: Read a CSV file into Flex
                            Flex harUI Adobe Employee

                            There is no built-in support for xslx file.  I don't the format so I can't

                            help you with the decoding.  I'm pretty sure it is compressed so you will

                            probably need to involve ByteArray inflate() or uncompress() at some point.

                            • 11. Re: Read a CSV file into Flex
                              marioterzo Level 1

                              I am trying to use the code enclosed, :

                               

                                package
                                  {
                                      import flash.display.Sprite;
                                      import flash.net.URLLoader;
                                      import flash.net.URLRequest;
                                      import flash.events.Event;
                                  
                                      import flash.events.IEventDispatcher;
                                  
                                    public class LoadCSV extends Sprite
                                    {
                                         public function LoadCSV():void {
                                 
                                             var loader:URLLoader = new URLLoader();
                                             configureListeners(loader);
                                 
                                             var request:URLRequest = new URLRequest("http://www.???.com/path/to/csv/file.csv");
                                 
                                             try {
                                                 loader.load(request);
                                             } catch (error:Error) {
                                                 trace ("Unable to load requested document.");
                                             }
                                         }
                                 
                                         private function configureListeners(dispatcher:IEventDispatcher):void {
                                             dispatcher.addEventListener(Event.COMPLETE, completeHandler);
                                         }
                                 
                                         private function completeHandler(event:Event)
                                         {
                                             trace ( event.target.data );
                                         }
                                     }
                                 }

                               

                              If I want to read a csv file on my hard disk how to write the:  new URLRequest("http://www.???.com/path/to/csv/file.csv");  ??

                               

                              new URLRequest("myfile.txt")  gives  me a flux error...

                               

                              thanks

                              • 12. Re: Read a CSV file into Flex
                                Chandra2034 Level 1

                                create a file object and pass and pass it's nativepath in UrLRequest.

                                • 13. Re: Read a CSV file into Flex
                                  marioterzo Level 1

                                  thanks !!! now I try...

                                   

                                  but in my project I cannot import flash.filesystem.File;

                                   

                                  do you know what libraries I must add, and where to find it ??

                                   

                                  thanks... (adobe libraries are not like java ...)

                                  • 15. Re: Read a CSV file into Flex
                                    marioterzo Level 1

                                    I am searching the Api 'flash.filesystem', where I can find it ? I must pay ?

                                     

                                    thanks

                                    • 16. Re: Read a CSV file into Flex
                                      Chandra2034 Level 1

                                      I did not get your question...

                                      • 17. Re: Read a CSV file into Flex
                                        marioterzo Level 1

                                        I need to use the class File, so I must write 'impot flash.filesystem.File' , but my system does not see this package...

                                        • 18. Re: Read a CSV file into Flex
                                          Chandra2034 Level 1

                                          Yeah... seems like  I was using Flash Builder 4.

                                          • 19. Re: Read a CSV file into Flex
                                            marioterzo Level 1

                                            yes, I am using just flash bulder 4..

                                             

                                            this is not a public library ?????????

                                            • 20. Re: Read a CSV file into Flex
                                              marioterzo Level 1

                                              I have found, at last, using URLLOADER. And I have understood that the library flash.filesystem is only for AIR ....

                                              • 21. Re: Read a CSV file into Flex
                                                marioterzo Level 1

                                                another question : if I want to read from my desktop what I must write instead of :

                                                 

                                                 

                                                 

                                                var request:URLRequest = new URLRequest("myfile.txt");

                                                 

                                                 

                                                 

                                                 

                                                thanks....

                                                • 22. Re: Read a CSV file into Flex
                                                  marioterzo Level 1

                                                  any suggestions to read from the desktop ?

                                                   

                                                  var request:URLRequest = new URLRequest("myfile.txt");

                                                   

                                                   

                                                  thanks...

                                                  • 23. Re: Read a CSV file into Flex
                                                    marioterzo Level 1

                                                    I have found the solutions to my problems, read a file .csv (using Flex and Flash Builder 4).

                                                    To read and process a text file I have used 'flash.net.FileReference';

                                                     

                                                    "The FileReference class provides a means to upload and download files between a user's computer and a server.
                                                    An operating-system dialog box prompts the user to select a file to upload or a location for download.
                                                    Each FileReference object refers to a single file on the user's disk and has properties that contain information
                                                    about the file's size, type, name, creation date, modification date, and creator type (Macintosh only)."

                                                     

                                                    "The FileReference Object first open up a file browsing dialog window on the user's machine with the browse() method and
                                                    prompt the user to select a local file to upload. Once the user select the file to upload, the FileReference Object
                                                    will upload the selected file to a web server that is specified in the upload() method. Once the file is uploaded
                                                    by the FileReference Object, it will not be uploaded to the web server immediately, instead it will be uploaded
                                                    to the browser's temp directory."

                                                     

                                                    Then, you need a server side script, that comes in and it will check the existence of the upload file.


                                                    If the file exists then the server side script will move the file from the browser's temp directory to
                                                    the specify directory of the server.

                                                     

                                                    For this I have used Apache commons 'fileUpload', writing a java jsp.

                                                     

                                                    The file csv must stay in the same directory as your published files.