开发者

How does SourceForge's download page work?

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 Java EE 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 d开发者_如何学Coes SF do that? - cause the browser to invoke the document Servlet.


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"></form>
    </body>
</html>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜