1 2 Previous Next 23 Replies Latest reply: Sep 29, 2012 10:38 PM by 963705 RSS

    How to force browser to open/save/save as the file from server

    963705
      Hi,

      How to force browser to open/save/save as the file from server instead of browser cache.

      I am creating a csv file through a pl/sql procedure and forwarding the link to user once user clicks on link he downloads the file, however if the same thing is repeated then browser

      returns the old cached file instead of new file generated on server.

      Please help.
        • 1. Re: How to force browser to open/save/save as the file from server
          jtahlborn
          include the appropriate "no cache" headers in your initial response, example 3-8 here:

          http://onjava.com/pub/a/onjava/excerpt/jebp_3/index2.html
          • 2. Re: How to force browser to open/save/save as the file from server
            963705
            jtahlborn wrote:
            include the appropriate "no cache" headers in your initial response, example 3-8 here:

            http://onjava.com/pub/a/onjava/excerpt/jebp_3/index2.html
            I have already used
            res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); //HTTP 1.1
            res.setHeader("Pragma","no-cache"); //HTTP 1.0
            res.setDateHeader ("Expires", 0); //prevents caching at the proxy server
            but browser is still showing old cached file.

            My servlet calls a procedure to generate a csv file and return the link for the same.
            Now servlet gives the link to user for downloading, user clicks on link to open/save/save as the file.
            If user selects open file option then first time it works ok but 2nd time onwards the browser shows the cached file instead of downloading from server.
            • 3. Re: How to force browser to open/save/save as the file from server
              jtahlborn
              well, first i'd validate that the headers actually are on the initial download response (using a proxy or some browser-side tool to examine the response).

              if that's still not working (although it should), i would guess that you are making a GET request and could try switching to a POST instead.
              • 4. Re: How to force browser to open/save/save as the file from server
                EJP
                but browser is still showing old cached file.
                It could be showing a version from before you added those headers. Flush caches, repeat, and repeat again. You should get two distinct downloads.
                • 5. Re: How to force browser to open/save/save as the file from server
                  963705
                  EJP wrote:
                  but browser is still showing old cached file.
                  It could be showing a version from before you added those headers. Flush caches, repeat, and repeat again. You should get two distinct downloads.
                  I have already done that. This problem occurs only when user is opening the csv file directly.
                  If he uses save as option then it works normally.

                  I have checked the headers are in the response, as stated above the problem arises only when user clicks open option to open the file directly without saving.

                  I am using post method as shown below
                  <form NAME=\"FORM1\" METHOD=\"POST\" ACTION=\""+js_pth+"\" >

                  Edited by: 960702 on Sep 26, 2012 11:12 AM
                  • 6. Re: How to force browser to open/save/save as the file from server
                    gimbal2
                    And does that happen in different browsers I wonder?

                    A quick hack I've used in the past to get flash movies to not cache (don't remember why but it was important) is to add a random number as an url-parameter; that forced the browser to reload the resource on each refresh because the url was unique. You might try something similar.
                    • 7. Re: How to force browser to open/save/save as the file from server
                      963705
                      gimbal2 wrote:
                      And does that happen in different browsers I wonder?

                      A quick hack I've used in the past to get flash movies to not cache (don't remember why but it was important) is to add a random number as an url-parameter; that forced the browser to reload the resource on each refresh because the url was unique. You might try something similar.
                      Site works well in IE hence no other browser used.
                      I am giving link to a file whose content gets updated but name is constant.
                      So when user clicks the direct link, he gets a browser open/save/save as box.
                      Things work well with save as but give problem with open option.
                      If user clicks on open option then first time things work well but 2nd time onwards browser starts picking old cached file.
                      • 8. Re: How to force browser to open/save/save as the file from server
                        gimbal2
                        yes you already said that, repeating information is not going to net you different answers.
                        • 9. Re: How to force browser to open/save/save as the file from server
                          963705
                          gimbal2 wrote:
                          yes you already said that, repeating information is not going to net you different answers.
                          Following statement asks me to change the URL-parameter, however in this case it is actually a direct link to a file present on server that to with a constant name.
                          A quick hack I've used in the past to get flash movies to not cache (don't remember why but it was important) is to add a random number as an URL-parameter; that forced the >browser to reload the resource on each refresh because the URL was unique. You might try something similar.
                          This is why I restated my concern :)
                          • 10. Re: How to force browser to open/save/save as the file from server
                            jtahlborn
                            at this point, it sounds like you have a browser specific problem (what version of IE are you testing)? you should definitely try another browser. is the browser actually making the second request to the server? if so, the issue may not be the browser caching, so much as however the file is being saved to disk in order to load is the problem. maybe the file isn't getting overwritten properly. or maybe you aren't refreshing the program you are using to read the file (excel perhaps?).
                            • 11. Re: How to force browser to open/save/save as the file from server
                              handat
                              960702 wrote:
                              I have already done that. This problem occurs only when user is opening the csv file directly.
                              If he uses save as option then it works normally.
                              This means the cached version is probably served by your web container's cache in which case you need to disable server side caching. Most web servers have this enabled by default.
                              • 12. Re: How to force browser to open/save/save as the file from server
                                963705
                                jtahlborn wrote:
                                at this point, it sounds like you have a browser specific problem (what version of IE are you testing)? you should definitely try another browser. is the browser actually making the second request to the server? if so, the issue may not be the browser caching, so much as however the file is being saved to disk in order to load is the problem. maybe the file isn't getting overwritten properly. or maybe you aren't refreshing the program you are using to read the file (excel perhaps?).
                                Have tried with all versions of IE and it works same.
                                First page asks for filter criteria and on the basis the file is generated and link is given to user.
                                Now to get different file you have to select different criteria, based on which new file is generated.
                                So user has to get to first page to change criteria and get new file.
                                And each time I am closing the excel before reopening the new file.
                                Still no help.
                                • 13. Re: How to force browser to open/save/save as the file from server
                                  963705
                                  handat wrote:
                                  960702 wrote:
                                  I have already done that. This problem occurs only when user is opening the csv file directly.
                                  If he uses save as option then it works normally.
                                  This means the cached version is probably served by your web container's cache in which case you need to disable server side caching. Most web servers have this enabled by default.
                                  As stated earlier also this happens only when clicking on open button. save as and save are working fine.
                                  • 14. Re: How to force browser to open/save/save as the file from server
                                    jtahlborn
                                    960702 wrote:
                                    handat wrote:
                                    960702 wrote:
                                    I have already done that. This problem occurs only when user is opening the csv file directly.
                                    If he uses save as option then it works normally.
                                    This means the cached version is probably served by your web container's cache in which case you need to disable server side caching. Most web servers have this enabled by default.
                                    As stated earlier also this happens only when clicking on open button. save as and save are working fine.
                                    i don't think you answered this question yet. when you see the old file again, does the browser actually make a request to your servlet or not?
                                    1 2 Previous Next