views:

190

answers:

2

In answers to other questions it was pointed out that SourceForge's download page worked by adding a hidden <iframe>. This does no longer seem to be the case...

How is the current version of the download page implemented? I'd like to build something similar because I consider SF's solution quite elegant.

Ok, more precisely...True, the initial "question" was too vague.

If you go to http://sourceforge.net/projects/beankeeper/files/beankeeper/2.6.2/beankeeper-2.6.2.tar.gz/download there's a plain old HTML link to download the file in question but there's also an automatic download. The delay seems to be some 2s.

Someone asked for my particular use case. I'll answer that knowing that it might divert from the OP to a certain extent. Very much simplified:

  • more or less traditional JEE app with two Servlets
  • a dispatcher Servlet which acts as an entry point for the UI controller
  • a "document Servlet" which streams PDF to the browser, it does so with content-disposition=attachment i.e. browser shows save-or-open dialog
  • a print out page with a number of input fields and a submit button
  • when the submit button is clicked
    • the input field values should be updated in the UI model (and session where necessary) -> that's what the dispatcher Servlet does
    • the creation of the PDF must be triggered -> that's what the document Servlet does

If I adopted SF's model a form submit would trigger the dispatcher Servlet and reload the current page (regular behavior). Upon reload of the page I would somehow - right, how does SF do that? - cause the browser to invoke the document Servlet.

+2  A: 

This is strictly MHO, but I personally do not find neither SourceForge's, nor CodePlex's download pages particularly elegant. Take a look at Google Code: clicking a hyperlink initiates download immediately, without asking you to accept a license agreement of any kind, select download mirror or view dozens of banners. It just does what it has to do: allow users to download whatever file they're interested in.

Anton Gogolev
I totally agree, that's how normal direct linking to files works: the best user experience with the least effort. However, many sites are not aiming for the best user experience, they're aiming to show the user an extra page full of adverts.
bobince
+2  A: 

Just change the window location during page load.

Here's an SSCCE, just copy'n'paste'n'run it.

<!doctype html>
<html lang="en">
    <head>
        <title>SO question 2222034</title>
        <script>
            window.onload = function() {
                setTimeout(function() {
                    window.location = 'http://download.java.net/maven/1/jstl/jars/jstl-1.2.jar';
                }, 2000); // It's "cool" to let user wait 2 more seconds :/
            }
        </script>
    </head>
    <body>
        <p>The download of jstl-1.2.jar will start shortly...</p>
    </body>
</html>

Or if you need to use POST, just submit a hidden form:

<!doctype html>
<html lang="en">
    <head>
        <title>SO question 2222034</title>
        <script>
            window.onload = function() {
                setTimeout(function() {
                    document.getElementById('downloadform').submit()
                }, 2000); // It's "cool" to let user wait 2 more seconds :/
            }
        </script>
    </head>
    <body>
        <p>The download of jstl-1.2.jar will start shortly...</p>
        <form id="downloadform" action="http://download.java.net/maven/1/jstl/jars/jstl-1.2.jar"&gt;&lt;/form&gt;
    </body>
</html>
BalusC
Nice, thanks.I missed the point that window.location = <url> does not alter the current page in the window if <url> does not point to a resource renderable by the browser.I'm sure decent (that's subjective of course) libraries like jQuery or Dojo offer to display some progress-like dialog while waiting for the resource. I wouldn't load a static resource that's already available on the server but invoke a Servlet which needs to create a PDF first. That mike take a few seconds. So, I don't have the 2s delay/timeout but a "natural" delay on the server. The user should be notified about this.
Marcel