Link to download, not opening a new window

Discussion in 'HTML & Website Design' started by Cobnut, Apr 10, 2008.

  1. #1
    OK, this is a damn silly question and I should know the answer but well, I don't.

    I've got an anchor linking to a .wmv movie file in an HTML document and when the link (an image) is clicked, the browser is navigating to the file, then offering the download box. What I want it to do is simply offer the download straight away, not navigate away from the page.

    This seems to happen OK when the resource link is outside the current domain but other than that I'm stumped.

    Look here http://www.theraincatcher.co.uk and click on the 'Play Video' image bottom-right to see what I mean.

    I didn't build or design this site, by the way, so please ignore the HTML. :)

    Jon
     
    Cobnut, Apr 10, 2008 IP
  2. Kirkbride

    Kirkbride Peon

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    There's nothing you can do to prevent that with straight HTML as far as I know. The only solution that's worked for me in similar situations is to create a download script/file in PHP (or another scripting language) which forces a download by setting the proper headers.

    So instead of href="movie.wmv" your link would be something like href="movie_download.php"

    And the movie_download.php file would contain code something like so:

    
    <?php
    
    header("Content-Type: application/octet-stream");
    header("Content-Disposition: attachment; filename=movie.wmv");
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: " . filesize("movie.wmv"));
    
    readfile("movie.wmv");
    
    ?>
    
    Code (markup):
    That's a simplified example which may need some tweaking to work.

    If you want or need to use something besides PHP, try searching the web for "force download [your scripting language]". You'll probably find plenty of examples/tutorials.
     
    Kirkbride, Apr 10, 2008 IP
  3. Cobnut

    Cobnut Peon

    Messages:
    184
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks Kirkbride, I'm familiar with PHP scripting for downloads (I actually have a module of my own that I drop into my own sites) but this is a very simple site and not PHP based. Since it's not 'mine' I'm reluctant to start offering to script PHP 'cos that's a road that never ends...

    Do you know why it opens immediately when the resource is 'off domain'?

    Jon
     
    Cobnut, Apr 10, 2008 IP
  4. Kirkbride

    Kirkbride Peon

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I hear you about being reluctant to offer that.

    Sorry, can't say that I know why it works when it's off domain. By that do you mean that the movie file is on another site? Or that you're linking to the file from another site?
     
    Kirkbride, Apr 10, 2008 IP
  5. melon

    melon Peon

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    If your goal is to let your visitors not open but download the wmv file, then you should use php to alter the header or configure mime types in the webserver config.
     
    melon, Apr 10, 2008 IP
  6. Cobnut

    Cobnut Peon

    Messages:
    184
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #6
    This video is used on another site but there the link is to a different TLD and when clicked, the link instantly brings up the download/open dialogue, without navigating away.

    I've poured over the code (such as it is - it's just HTML) and there's no physical difference in the tagging hence the guess that it's something to do with a resource 'from afar' as it were.

    To be honest I think that the download/open method is ugly and un-user friendly anyway and I've advised that they have a page with the video properly embedded. As usual, their budget won't cover the cost of a cup of coffee so whether they run with that idea is open to debate :)

    Jon
     
    Cobnut, Apr 11, 2008 IP
  7. Kirkbride

    Kirkbride Peon

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    That's a good call on embedding the video. The link graphic does say "Play Video", not "Download Video" after all. Good luck getting your client to take your advice.
     
    Kirkbride, Apr 11, 2008 IP