Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
46
rated 0 times [  49] [ 3]  / answers: 1 / hits: 45859  / 13 Years ago, wed, april 13, 2011, 12:00:00

Is there a way to make an image a download once you click on it (without right-click save image as)?



I'm using a small Javascript function to call the download page:



<a href=# 
onclick=window.open('download.php?file=test.jpg', 'download', 'status=0');
>Click to download</a>


In the download.php page I have something like:



$file = $_GET['file'];
header('Content-Description: File Transfer');
header(Content-type: image/jpg);
header(Content-disposition: attachment; filename= .$file.);
readfile($file);


But it doesn't work. What am I doing wrong?

Thanks in advance!


More From » php

 Answers
2

Use application/octet-stream instead of image/jpg:



If [the Content-Disposition] header is used in a response with the application/octet-stream content-type, the implied suggestion is that the user agent should not display the response, but directly enter a `save response as...' dialog.

RFC 2616 – 19.5.1 Content-Disposition



[#92757] Tuesday, April 12, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alli

Total Points: 409
Total Questions: 101
Total Answers: 105

Location: The Bahamas
Member since Tue, Apr 27, 2021
3 Years ago
alli questions
Sat, Apr 23, 22, 00:00, 2 Years ago
Mon, May 18, 20, 00:00, 4 Years ago
Tue, Mar 24, 20, 00:00, 4 Years ago
Fri, Jan 24, 20, 00:00, 4 Years ago
;