tags:

views:

40

answers:

3
+4  Q: 

html download tag

The basic problem is this: I have a link to an image file. The desired behavior when I click on the link is to get the download dialog which would allow me to launch an associated image editor. This doesn't happen because the image file is rendered by the browser.

Is there any html magic which I can use to force the browser to offer a download dialog when the user clicks on a link?

Any help or pointers would be much appreciated.

+1  A: 

Not possible with just HTML however you can force the file to be downloaded via modifying headers with a server side language such as PHP.


// grab the requested file's name
$file_name = $_GET['file'];

// make sure it's a file before doing anything!
if(is_file($file_name))
{

    /*
        Do any processing you'd like here:
        1.  Increment a counter
        2.  Do something with the DB
        3.  Check user permissions
        4.  Anything you want!
    */

    // required for IE
    if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off'); }

    // get the file mime type using the file extension
    switch(strtolower(substr(strrchr($file_name,'.'),1)))
    {
        case 'pdf': $mime = 'application/pdf'; break;
        case 'zip': $mime = 'application/zip'; break;
        case 'jpeg':
        case 'jpg': $mime = 'image/jpg'; break;
        default: $mime = 'application/force-download';
    }
    header('Pragma: public');   // required
    header('Expires: 0');       // no cache
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ($file_name)).' GMT');
    header('Cache-Control: private',false);
    header('Content-Type: '.$mime);
    header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
    header('Content-Transfer-Encoding: binary');
    header('Content-Length: '.filesize($file_name));    // provide file size
    header('Connection: close');
    readfile($file_name);       // push it out
    exit();

}
Brady
But does that mean that simply by navigating to this page that the user will be forced to download the image?
Gearoid Murphy
No what you would have to do is change your image link to point to this php file sending via $_GET['file'] where the image is. and then yes the file will be forced to download
Brady
It's mediawiki, so I'm not sure I can easily manipulate the http headers from the context of a mediawiki-extension
Gearoid Murphy
No what you would do is save this code as a php file and place it in your images folder. then to force an image to download you link to the file like:http://www.example.com/images/downloadimage.php?file_name=myimage.jpginstead of http://www.example.com/images/myimage.jpgNo need to modify code mediawiki
Brady
A: 

if you want to show up download dialog, you need to put special headers to your image

in PHP it will be:

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=image.jpg');
header('Content-Transfer-Encoding: binary');
Dobiatowski
A: 

Another solution would be to add this to an htaccess file in the folder with your images:

AddType application/octet-stream .jpg

Brady
Wouldn't that force all jpg files to be treated as raw binary?, I'd like to be able to render the images in the browser as well...
Gearoid Murphy
yes it would force all images to be downloaded. With this solution you would put the images you want to be forced downloading into a seperate folder and apply this code to that folder only. If you cannot do this for what ever reason then a PHP or similar solution is the only way.
Brady