Hi.. We have href= linked to image paths. We dont want the clicks to open the image on the window. We want it to prompt for download, or save as for the users. Whats the trick ?
Don't use an <a> tag - that's for opening that link. Or have more intelligent users, who know to right-click the link and save the content.
I don't think you can do much with JS, perhaps a PHP "proxy" script with certain headers would do that.
Try to do this via webserver configuration instead of javascript. http://drupal.org/node/417866 Btw. If you need JS solution: I believe it is possible to register javascript function which forbids opening an image and which creates hidden iframe with src="yourdomain/downloadfile.php?file=image1" and PHP (or another server side language) will send it to browser with HTTP header Content-Disposition attachment...
simply return the image with the header information for it to be downloaded. here: http://php.net/manual/en/function.header.php i honestly hate to give out free scripts that can be used as is, because it doesn't guarantee that you will learn something. so promise me that you will read it over, and not just copy and paste it. <?php $file = 'path/to/image/file.jpg'; if (file_exists($file)) { header('Content-Description: File Transfer'); header('Content-Type: download/jpg'); header('Content-Disposition: attachment; filename='.basename($file)); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . filesize($file)); ob_clean(); flush(); readfile($file); exit; } else { echo 'Error: file does not exist'; } ?> Code (markup):
Yep - as hotnoob said, you have to run it through some sort of server side script, which can prompt the file as a download. The other alternative is creating an htaccess file or configuring your server to treat certain filetypes as ocet-streams, so that the user is prompted to download it.