14 Replies Latest reply on Aug 23, 2006 4:49 AM by Usinde

    How do I create a downloadable file in Flex without going back to the sever

    rkschulz
      In short: In need a way of generating the content of a downloadable file from
      withing a Flex app rather than streaming content from a server, i.e

      FileReference.download(<ClientSideFlexGeneratedContent>)

      FileReference.download only takes a URL as far as I can see.
      Is there a way of achieving this?


      Here is the full story:
      I am looking into building a reporting app that lets the user filter and sort based on an XML data island.
      Am using code to tranform the XML and the results get displayed in the datagrids. All that is done in
      memory on the client side (I load the XML once in the beginning).

      Users want to be able to "export" (i.e. File-download) the content of a data grid into a local csv file.
      FileReference.download() is sort of what I want expect that I don't want to go back to the server but
      have the Flex app build the content on the downloadable file on the fly out of memory (based on the
      XML I have).

      So - in Java speak - I need something like FileReference.download(OutputStream). So that I can
      populate the stream from within my FlexApp. Going back to the server would be a serious pain,
      because the filtering logic isn't on the server and I would have to double code all logic, ship the fitler
      parameters back to the server, re-generate what I already have on the client and then stream it down.

      Any help / ideas are much appreciated.
      Thanks,

      Robert.






        • 1. Re: How do I create a downloadable file in Flex without going back to the sever
          peterent Level 2
          I've been down this road before, and it isn't possible right now. It is on our list for the future as this is a much-desired feature. We are limited to what the browser environment offers. Plus we have a deep committment to maintain a high standard of security. Allowing Flash applications to read and write files directly to the user's hard drive would violate that trust. So part of our future task is to figure out how to do this in keeping with the spirit of our security efforts.
          • 2. Re: How do I create a downloadable file in Flex without going back to the sever
            rkschulz Level 1
            Peter - thanks for your reply.

            I agree with your comment on security - if Flash is perceived to be a security issue, people will disable
            it and that's no good to anyone.

            I don't quite see however why what I want is any different to what you get with the FileReference.download call. It doesn't really matter where the content comes from (straight from the server or from the server to the flex app and then into a file). Is the problem that the browsers don't expose the functionality once you are "in" a document?

            I haven't really played with this, but is there a path using the ActionScript -> JavaScript ExternalInterface? I am thinking with some funky JavaScript I could create a new document and then pump data from Flex to JavaScript and from there into the document. I would be a bit clunky to ask the use to save the document into a text file but that's probably ok. Comments or thoughts on this anybody?

            All I want to do is put a button under the datagrid views and after the user filtered and sorted that data let them save the content in a comma separate file so they can do other go off and use it in Excel or whatever.

            Another way is to pump it all into the clipboard - is there a limit how much data you can store there from Flex?

            Any other ideas how I could get there?

            Thanks,

            R.
            • 3. Re: How do I create a downloadable file in Flex without going back to the sever
              peterent Level 2
              This is really a tough one. I definitely see your point with the file download. There may be an additional security issue that I'm not aware of. But I'll ask around and see if anyone has some ideas as to how to accomplish what you want to do.
              • 4. Re: How do I create a downloadable file in Flex without going back to the sever
                rkschulz Level 1
                Thanks Peter - please keep me posted if you find anything.

                I guess I'll push ahead with Flex anyway (we are evaluating it at the moment) - it works pretty damn well given that it's fairly new (and I have no clue how to use it all yet ;-) I have XML docs with a few thousand nodes max and so far filtering / sorting / displaying works well and seems fast enough. I think I'll probably get away with a stateless server architecture (and I do like the sound of that). That's one of the reasons that I really want to get the download straight out of Flex working somehow. I could of course shuttle everything back and have a "turn-around" servlet that spits the content back (I assume the file reference supports a POST request as the server URL) but there is something just not quite right about this approach ;-)

                So - if you hear anything, please let me know. Maybe the Flex -> JavaScript bridge may be a go somehow.

                R.
                • 5. Re: How do I create a downloadable file in Flex without going back to the sever
                  peterent Level 2
                  You do have limited options.

                  One is to use ExternalInterface to hand the data over to JavaScript and hope that you can find a 3rd party product which enables JavaScript to write a local file.

                  The other is to send the data to a server and then use FileReference to download it.

                  Security, for use, is extremely important, even at the cost of some functionality. We'd rather lose a feature than compromise security. But I do know that the folks here understand the need to export data.
                  • 6. Re: How do I create a downloadable file in Flex without going back to the sever
                    Matlus
                    JavaScript will not allow you to do anything locally. So that's out of the question.

                    What I think you should do is save the parametes of the "query" in session (or depending on the size of data, the dataset can be in session). When the user wants the file, you simply send back the file using the query parameters or the dataset. This is how we do it currently (with or without Flex, there is not difference).

                    Shiv.
                    • 7. Re: How do I create a downloadable file in Flex without going back to the sever
                      Matlus Level 1
                      JavaScript will not allow you to do anything locally. So that's out of the question.

                      What I think you should do is save the parametes of the "query" in session (or depending on the size of data, the dataset can be in session). When the user wants the file, you simply send back the file using the query parameters or the dataset. This is how we do it currently (with or without Flex, there is not difference).

                      Shiv.
                      • 8. Re: How do I create a downloadable file in Flex without going back to the sever
                        Matlus Level 1
                        JavaScript will not allow you to do anything locally. So that's out of the question.

                        What I think you should do is save the parametes of the "query" in session (or depending on the size of data, the dataset can be in session). When the user wants the file, you simply send back the file using the query parameters or the dataset. This is how we do it currently (with or without Flex, there is not difference).

                        Shiv.
                        • 9. Re: How do I create a downloadable file in Flex without going back to the sever
                          rkschulz Level 1
                          Shiv - thanks for your comments, won't work (easily) for me though.

                          One of the reasons that I am interested in Flex is that it lets me keep all the state on the client (in the flex app). This approach should work well for a "reporting" type application (because I only need to get an data "island" once and can then do everything on the client). So I don't even have the logic to the filtering and sorting on the server and there is no server session as such. In that sense Flex or no Flex makes a difference. In "traditional" web apps you would have the model on the server and the view on the client and then the export is just another view. But with Flex, you can have both the model and the view on the client, so providing the export as an alternative view from the server does not work (you have to transfer your model back to the server first).

                          So, Flex really does need a way of populating the download content on the FileReference client-side (or something to that effect).

                          R.
                          • 10. Re: How do I create a downloadable file in Flex without going back to the sever
                            Matlus Level 1
                            Robert,

                            Keeping in mind Peter's point about security and now having a better idea of what your requirements/constraints are there is another option...

                            You could make an activeX control that plug's into the browser just like the Flash palyer does. You use this ActiveX control to provide you with access to the local machine. You can communicate between your Flex app and this activex control via Javascript.

                            So essentially, this activeX control surfaces an API allowing you to do what you need for it to do. This activeX control doesn't need to have any visual aspect so your users don't even see it. They will need to download and install it much like the Flash player. You/your company can "sign" it so they see it's from you/your company.

                            We've used this approach a few times ourselves when browser/JavaScript security "comes in the way".

                            I know it's not "ideal" but you never know what else you might need this activex control to do for you in the future :).

                            Shiv.
                            • 11. Re: How do I create a downloadable file in Flex without going back to the sever
                              Matlus Level 1
                              Robert,

                              A few other options:
                              1. You have a method on the server that simply receives an xml document and transposes it into a CSV file and sends it back to the client. This method requires no state, model or anything.

                              2. Send the xml document over to JavaScript. JavaScript can then convert that "data" into a CSV "html document" using the document.write method. (meaning the user essentially sees it as a page that contains "text" they can then choose to save the file locally at which point it's a simple CSV file.

                              3. Email the CSV file to the user :). I don't know if Flex has any SMTP socket out of the box, but once again, the server side processing can be completely "dumb".

                              4. If the user has Excel you could save the user a step and open up Excel and hand it the xml document (using JavaScript).

                              Shiv.
                              • 12. Re: How do I create a downloadable file in Flex without going back to the sever
                                rkschulz Level 1
                                Hi Shiv - thanks for the answers. Am not a huge fan of the ActiveX approach. The other approaches are similar to what I was thinking:

                                1) I guess is the "cleanest" solution from a security point of view - haven't had a chance to test it but am hoping the FileReference.download supports a URL request that does a post, so I can have a "turn around" servlet that just
                                writes it back. It's just seems like such a waste roundtripping everything.

                                2) Should work, but it would be quite clunky to ask the user to "Save as ..." the page. I'll have to play with it and see how painful it will be.

                                3) I don't like that - don't think it will work from a client easily without making a mail server available somewhere and if the server sends it I am back to shipping the xml to the server anyway, plus the user has to wait.

                                4) How does 4 work? Do you have some sample JavaScript that does that?

                                Thanks agian,

                                R.
                                • 13. Re: How do I create a downloadable file in Flex without going back to the sever
                                  Matlus Level 1
                                  Robert,

                                  >Am not a huge fan of the ActiveX approach
                                  The Flash Player is an ActiveX control :) So what's one more.

                                  Addressing option 2:
                                  Another feature that is available if the user has Excel installed is the ability to right click on an html table and choose the "Export To Excel" option. This option will import the "data" in the html table directly into Excel.

                                  Excel:
                                  Well all the Microsoft Office applications are also ActiveX controls and therefore can be instantiated and run using JavaScript (provided the users have Ms Office installed on their machines).

                                  Barring the "communication between Flex and JavaScript (I've not done that kind of thing yet (brand new to Flex, Flash etc.), the JavaScript required to load an xml document (that is available in JavaScript) should not be too difficult to do. I've not done it (I use Microsoft Word a lot in this fashion) myself, but if that's the route you're tending towards, I could help you with it.

                                  Here is a link that has sample code that shows you how to Automate Excel
                                  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_wrcore/html/wroriautom atingexcelusingexcelobjectmodel.asp

                                  The code is in VB/VBscript but easily translated to JavaScript since you really only need to understand the Excel object model.

                                  Let me know if you need help.
                                  PS: If you change your mind about the ActiveX thing, let me know and I can cook up a little ActiveX control you can use.

                                  Shiv.
                                  • 14. Re: How do I create a downloadable file in Flex without going back to the sever
                                    Usinde
                                    Hi,
                                    I'm in a similar situation where I need to be able to export a summary report from Flex into MS Word - without invoking the server, additional plugins etc.
                                    I solved it partly by copy all textfield content to the clipboard and, after that, launching a MS Word template with a text similar to "Paste text here".
                                    However... I can't copy images (diagrams in my case) this way and it's not a very user-friendly approach alltogether either.
                                    If anyone get any new insights/ideas in this area it would be very helpful.

                                    /Jesper (first post)