0

I have a request to make a jpg image download in the same way a pdf file would.

At present if I add a jpg image to a web page as a link, it will open in another browser window rather than actually download to the users computer. However, a pdf file will.

Here is the standard code:

<a href="/images/my-image.jpg">download image</a>

I would appreciate any guidance on this.

Thanks.

forrest
  • 10,570
  • 25
  • 70
  • 132
  • 1
    You will need to modify your webserver configuration or write a script modifying the header when the file is sent to the browser. Browsers will always try to display images. – Cobra_Fast May 20 '11 at 14:20
  • 2
    Are you using any server-side language? If so, one way I can think of is to use the content-disposition header. http://stackoverflow.com/questions/1012437/uses-of-content-disposition-in-an-http-response-header – DeaconDesperado May 20 '11 at 14:20

5 Answers5

4

The best way to do this is to set your web server, or a server-side script (php, asp.net, etc) to serve your jpg file with the Content-Disposition header :

Content-Disposition: attachment; filename="my-image.jpg"

Script:

Servers:

Community
  • 1
  • 1
Colin Pickard
  • 45,724
  • 13
  • 98
  • 148
2

If you have PHP on the server you can do this.

<?php 
// Force download of image file specified in URL query string and which 
// is in the same directory as this script: 
if(!empty($_GET['img'])) 
{ 
   $filename = basename($_GET['img']); // don't accept other directories 
   $size = @getimagesize($filename); 
   $fp = @fopen($filename, "rb"); 
   if ($size && $fp) 
   { 
      header("Content-type: {$size['mime']}"); 
      header("Content-Length: " . filesize($filename)); 
      header("Content-Disposition: attachment; filename=$filename"); 
      header('Content-Transfer-Encoding: binary'); 
      header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
      fpassthru($fp); 
      exit; 
   } 
} 
header("HTTP/1.0 404 Not Found"); 
?>

HTML Would then look like this

<img src="/images/download.php?img=imagename.jpg" alt="test">
Mech
  • 2,904
  • 3
  • 24
  • 29
2

You can use the suggestion mentioned here: http://answers.yahoo.com/question/index?qid=20080417163416AAjlb7T

It would look something like this:

access_file.php:

<?php

$f= $_GET['file'];

$filename = explode("/", $f);
$filename = $filename[(count($filename)-1)];

header('Content-Disposition: attachment; filename="'.$filename.'"');
readfile($f);

?>

If you stick the access_file.php file in your images folder then you could just use the link:

<a href="/images/access_file.php?file=my-image.jpg" />
Bryce Siedschlaw
  • 4,136
  • 1
  • 24
  • 36
1

How PDF content type opens from your browser is determined by a number of different settings. It could be influenced by content type associations to a program in your browser settings. It also may be influenced by settings in the PDF reader itself, for instance, Adobe Reader under Edit, Preferences..., Internet tab has an option for if PDF documents should open in the browser or in the application directly.

maple_shaft
  • 10,435
  • 6
  • 46
  • 74
0

In HTML 5 we have an attribute called download

<a href="img/myimage.jpg" download="myimage.jpg"><img src="img/myimage.jpg" /></a>