Topic
16 replies Latest Post - ‏2013-01-24T01:53:17Z by SystemAdmin
SystemAdmin
SystemAdmin
6195 Posts
ACCEPTED ANSWER

Pinned topic How to save a generated csv file to the remote PC (web browser)

‏2012-10-29T17:51:31Z |
I am currently generating CSV files, which are created on the OS where WAS resides (currently linux). I'm trying to figure out how to save these files local to the Windows laptops that the users will be accessing my RUI from. I'm thinking that I'd somehow need to trigger the browser's "Save As" capability, but not sure beyond this. Any help would be appreciated.
Updated on 2013-01-24T01:53:17Z at 2013-01-24T01:53:17Z by SystemAdmin
  • dan_darnell
    dan_darnell
    973 Posts
    ACCEPTED ANSWER

    Re: How to save a generated csv file to the remote PC (web browser)

    ‏2012-10-29T20:12:53Z  in response to SystemAdmin
    You may need to describe in further detail what you would like to see happen. My initial take, based on the description you've given, is this: give the user a link to the file you generate (you can create this link dynamically if desired/required) and they can use their browser to "save as" the file or they can simply click on the link and let their PC make the decision about what to do with it (open it with a compatible plug-in, or with Excel, or however their PC is configured to handle CSV content).

    If that won't do the trick please describe in more detail your design ambitions.

    --Dan
  • SystemAdmin
    SystemAdmin
    6195 Posts
    ACCEPTED ANSWER

    Re: How to save a generated csv file to the remote PC (web browser)

    ‏2012-10-29T20:36:17Z  in response to SystemAdmin
    Ideally I'd like the file not to be created on the local OS, where WAS sits (ie Linux in this case), but if it is much easier or necessary, I can schedule a cleanup on a regular basis to clear the folder. I am open to any solution where the csv file is more accessible to the user on their web browser, versus needing to request a copy of it be sent to them by the system admins. The solutions that you mentioned are fine for this - either open as an Excel automatically, or trigger a 'save as' where they can choose the location on their PC to save the file.
    • dan_darnell
      dan_darnell
      973 Posts
      ACCEPTED ANSWER

      Re: How to save a generated csv file to the remote PC (web browser)

      ‏2012-10-29T20:53:12Z  in response to SystemAdmin
      I'm going switch up terms on you...

      local OS = user's PC
      server OS = place where WAS sits

      Let's look at two scenarios:

      1. CSV file created on the local OS (remember, when I say "local" I mean the user's PC).

      You can do this with something called a data URI. See http://en.wikipedia.org/wiki/Data_URI_scheme. I've never done it in EGL but I'm going to try it and I will post a sample if I get it working.

      Using this approach you could pass raw data to your application client and have it create a CSV file that is downloadable when a link is clicked or a button is pressed. The server would not generate the CSV file so it would not have to be cleaned up later.

      2. CSV file created on the server OS.

      This is probably the more practical approach because it doesn't involve passing a bunch of data to the client and having the client build a data URI. (Approach #1 sounds cool -- and I am going to try it -- but, again, not very practical for CSV files of any size.) So with approach #2 you would continue to generate your CSV files on the server and then offer them up to a client as a link. When we do this we have a plain old Apache HTTP server serving up the files. And yes, we schedule a cleanup process to clear the folder where the temporary CSV files land. Security is always a consideration. It's no biggie to serve the data over SSL but how important is it in your application that the CSV files be accessible only to an authenticated client?

      --Dan
      • dan_darnell
        dan_darnell
        973 Posts
        ACCEPTED ANSWER

        Re: How to save a generated csv file to the remote PC (web browser)

        ‏2012-10-29T21:36:58Z  in response to dan_darnell
        Okay, that was fun. Here is some code to make a CSV file available as a link without any server-side involvement whatsoever. (Jump to after the code for caveats.)

        
        
        
        import com.ibm.egl.rui.widgets.*;   handler Test type RUIhandler 
        {initialUI = [ ui ], onConstructionFunction = start 
        } ui Box 
        { children = [mylink] 
        }; mylink HTML 
        { 
        }; function start() 
        // Raw CSV data composed in the client myCSVdata string = 
        "123,456,789\r\n987,654,321\r\n"; 
        // File name to use for the downloaded file                myFilename string = 
        "hereisyourfile.csv"; 
        // Data URI construction (includes URL encoding the CSV data) dataURI string = 
        "data:text/csv;charset=utf-8," + ServiceLib.convertToURLEncoded(myCSVdata); 
        // Create a link with the data URI as a target mylink.text = 
        "<a download=\"" + myFilename + 
        "\" href=\"" + dataURI + 
        "\">Click here for very special CSV file</a>"; end end
        


        With this sort of solution you could take raw data in the client and format it as CSV data then make it available for download as a link. No CSV file is ever created on the server.

        However...

        This type of data URI is not supported in all browsers or all browser versions. (For example, some version of IE only allow image data as a data URI. The wiki link I gave earlier has all the details on browser support.)

        The idea of file data as a data URI seems to have legs though so eventually this technique will (probably, maybe) work everywhere.

        The "download" attribute of the anchor tag (which allows us to specify a target filename) is supported in Chrome and possibly nowhere else. IE chokes on it (at least some versions of IE) and Firefox ignores it.

        I still wouldn't use the technique for a large amount of data.

        So probably not a viable technique for your needs but it does work...in some browsers.

        --Dan
  • SystemAdmin
    SystemAdmin
    6195 Posts
    ACCEPTED ANSWER

    Re: How to save a generated csv file to the remote PC (web browser)

    ‏2012-10-29T22:06:34Z  in response to SystemAdmin
    Thank you Dan. I'll look into both options further. Regarding a couple of your questions / comments. I am currently designing based on the assumption that security / authentication is not an issue. I expect that most users will be using IE 8, though Chrome is my personal favorite. I expect that most of my csv files will be less than 100K, thus fairly small.

    Given this, it sounds like your recommendation is with both a local and server copy of the data, though I can use your code to skip the server side. I don't know if it matters that I am currently creating writing this csv record via database reads.
  • SystemAdmin
    SystemAdmin
    6195 Posts
    ACCEPTED ANSWER

    Re: How to save a generated csv file to the remote PC (web browser)

    ‏2012-10-30T00:04:50Z  in response to SystemAdmin
    I tested the current code without success (via Tomcat web server) on both IE 8 & Chrome. The popup never comes. I'm assuming your option 2 really is the better (more stable option anyway). Can you please provide some guidance on how I provide a link to the file that I am creating on the server housing WAS?
    • dan_darnell
      dan_darnell
      973 Posts
      ACCEPTED ANSWER

      Re: How to save a generated csv file to the remote PC (web browser)

      ‏2012-11-02T15:55:56Z  in response to SystemAdmin
      Hi Eric,

      We use Apache HTTP Server to serve generated documents via Alias and Directory directives. For example, with the following in an Apache HTTP Server config...

      Alias /gendoc /gendoc                      
        <Directory /gendoc>                        
           Order Allow,Deny                         
           Allow From all                           
        </Directory>
      


      ...we can reference documents with a URL like this:

      www.myserver.com/gendoc/document.pdf

      (Generated documents go into a directory named "/gendoc" on the server.)

      From a Rich UI client, one approach would be to use HTML widgets:

      html HTML {text=" document.pdf"};

      The above would give you a clickable link to a document from a Rich UI client and open it in a new window or tab.

      You can create your HTML widgets dynamically and add them to a Box or Div or put them in a dynamically constructed grid based on the back-end documents you've stored.

      Note: If you want to have a pure WAS solution you can enable the file serving Servlet and configure it to serve your static content.

      --Dan
      Updated on 2014-03-25T04:39:13Z at 2014-03-25T04:39:13Z by iron-man
      • dan_darnell
        dan_darnell
        973 Posts
        ACCEPTED ANSWER

        Re: How to save a generated csv file to the remote PC (web browser)

        ‏2012-11-02T16:00:40Z  in response to dan_darnell
        Hi Eric,

        We use Apache HTTP Server to serve generated documents via Alias and Directory directives. For example, with the following in an Apache HTTP Server config...

        Alias /gendoc /gendoc                      
          <Directory /gendoc>                        
             Order Allow,Deny                         
             Allow From all                           
          </Directory>
        


        ...we can reference documents with a URL like this:

        www.myserver.com/gendoc/document.pdf

        (Generated documents go into a directory named "/gendoc" on the server.)

        From a Rich UI client, one approach would be to use HTML widgets:

        html HTML {text="<a href="#" target="\"_blank\"">document.pdf</a>"};
        


        The above will give you a clickable link to a document from a Rich UI client and open it in a new window or tab.

        You can create your HTML widgets dynamically and add them to a Box or Div or put them in a dynamically constructed grid based on the back-end documents you've stored.

        Note: If you want to have a pure WAS solution you can enable the file serving Servlet and configure it to serve your static content.

        --Dan
        Updated on 2014-03-25T04:39:08Z at 2014-03-25T04:39:08Z by iron-man
        • dan_darnell
          dan_darnell
          973 Posts
          ACCEPTED ANSWER

          Re: How to save a generated csv file to the remote PC (web browser)

          ‏2012-11-02T16:03:00Z  in response to dan_darnell
          Hey, that's cool (not). That didn't post to the forum correctly either!

          Let's try it as an attachment...

          Attachments

          • SystemAdmin
            SystemAdmin
            6195 Posts
            ACCEPTED ANSWER

            Re: How to save a generated csv file to the remote PC (web browser)

            ‏2013-01-21T19:06:21Z  in response to dan_darnell
            I put this development task on hold for a bit, and now I am back to working on it. I am able to serve files that are stored within the WAR file (on WAS 7.0) via a hyperlink, but what I am not able to figure out is how to point the hyperlink to a file that I have generated, and saved on the OS. From what I've read about the file serving widget, it appears that the files are served relative to the ear file name that is deployed. So I am thinking I need to either save the generated files to within the ear, which I'm pretty sure is not an option, or point the ear file back to a base folder on the OS.

            Thanks Dan, for the other solution of keeping the whole thing within the web session, but it won't work due to the browsers we are using, and the large size of the generated files. Can you provide guidance on how to access the OS files?

            Thanks,
            Eric
            • dan_darnell
              dan_darnell
              973 Posts
              ACCEPTED ANSWER

              Re: How to save a generated csv file to the remote PC (web browser)

              ‏2013-01-21T23:00:51Z  in response to SystemAdmin
              I think you should use a regular web server (Apache HTTP Server or IBM HTTP Server) for serving content like you want to do.

              In prior messages I outline the configuration required to do this using Apache.

              --Dan

              p.s. I did once, in a pinch, write a servlet that I deployed on WebSphere Application Server to serve static content from WAS in a more configurable way than the default file serving thing-a-mabob allows but I don't recommend that approach. Get a web server in the mix to handle your file serving needs.
              • dan_darnell
                dan_darnell
                973 Posts
                ACCEPTED ANSWER

                Re: How to save a generated csv file to the remote PC (web browser)

                ‏2013-01-21T23:06:05Z  in response to dan_darnell
                Oh, and another approach that I used once was to create a symbolic link under my exploded EAR/WAR in WAS. The symbolic link was to a directory where I put my generated content. Because the link appeared to be a part of the EAR/WAR, the default file serving thing-a-ma-bob in WAS happily served up content from it. I don't recommend the approach though because uninstalling/reinstalling the EAR/WAR was pretty screwy with a kludged-in symbolic link in the mix.

                --Dan
                • SystemAdmin
                  SystemAdmin
                  6195 Posts
                  ACCEPTED ANSWER

                  Re: How to save a generated csv file to the remote PC (web browser)

                  ‏2013-01-22T01:23:57Z  in response to dan_darnell
                  I will look into getting the IBM HTTP Server installed, but given that this is a shared environment with another production instance of unrelated software (which uses WAS 7.0 as it's base), I fear the customer will not be too open to this. In parallel to this, any chance you might remember specifics on the symbolic link approach?

                  Thanks,
                  Eric
                  • dan_darnell
                    dan_darnell
                    973 Posts
                    ACCEPTED ANSWER

                    Re: How to save a generated csv file to the remote PC (web browser)

                    ‏2013-01-23T17:54:21Z  in response to SystemAdmin
                    "From what I've read about the file serving widget, it appears that the files are served relative to the ear file name that is deployed. So I am thinking I need to either save the generated files to within the ear, which I'm pretty sure is not an option, or point the ear file back to a base folder on the OS."

                    In order to point back to a base folder (outside of the deployed EAR structure) you can create a symbolic link within the deployed EAR structure that points to the directory that contains your files. This way, as far as the file serving Servlet knows, it is serving content from within the deployed EAR structure.

                    --Dan
                    • SystemAdmin
                      SystemAdmin
                      6195 Posts
                      ACCEPTED ANSWER

                      Re: How to save a generated csv file to the remote PC (web browser)

                      ‏2013-01-24T00:32:14Z  in response to dan_darnell
                      Thanks Dan. I was able to get that working, but I see what you mean about it being tricky to create the symbolic link each time I deploy an update. Any chance you can make a suggestion for creating a hyperlink to the file that I generated?

                      This code works to give a popup with the file name:

                      popup_csv_success DojoDialog {
                      children = content_csv_success
                      };
                      content_csv_success Box { columns=1, children = box_text_csv_success };
                      box_text_csv_success.text = "The Archive CSV File " + csvName + " has been created successfully in " + csvWriteDir;

                      popup_csv_success.showdialog();
                      I was hoping to do something similar with a hyperlink, as follows, but the csvName variable doesn't work in this context:

                      content_csv_success Box { columns=1, children = HyperLink_archive };

                      HyperLink_archive HyperLink{ text= ""The CSV File " + csvName + " has been created successfully. ", href = "/BDRDEV/Extracts/RTL/" + csvName "};

                      I also tried to use the HTML sample that you provided in an earlier post, but I couldn't figure that out. All of this is in my RUI Handler.

                      Thanks again for you assistance and any more help you can provide.
                      • SystemAdmin
                        SystemAdmin
                        6195 Posts
                        ACCEPTED ANSWER

                        Re: How to save a generated csv file to the remote PC (web browser)

                        ‏2013-01-24T01:53:17Z  in response to SystemAdmin
                        Quick update. I got the HTML approach working - small typo in my code.