views:

592

answers:

2

Hey all,

I generate normal links like: <a href="/path/to/image"><img src="/path/to/image" /></a> in a web app.

When I click on the link, it displays the picture in a new page. If you want to save the picture, then you need to right click on it and select "save as"

I don't want this behaviour, I would like to have a download box popping out when I click on the link, is that possible just with html or javascript? How?

If not I guess I would have to write a download.php scrit and call it into the href with the file name as parameter...?

Greetings! :)

+4  A: 

No, it isn't. You will need something on the server to send a Content-Disposition header to set the file as an attachment instead of being inline. You could do this with plain Apache configuration though.

I've found an example of doing it using mod_rewrite, although I know there is a simpler way.

David Dorward
Hey, you say: "although I know there is a simpler way"... which one? ^_^
Piero
If I could find the details, I'd post them. I can't find them right now and don't have the time to devote to an extensive search.
David Dorward
It's fine thanks for your help. I just created a php page that generates the download...
Piero
+1  A: 

You can't do it with pure html/javascript. This is because you have a seperate connection to the webserver to retrieve a seperate file (the image) and a normal webserver will serve the file with content headers set so that the browser reading the content type will decide that the type can be handled internally.

The way to force the browser not to handle the file internally is to change the headers (content-dispsition prefereably, or content-type) so the browser will not try to handle file internally. You can either do this by writing a script on the webserver that dynamically sets the headers (i.e. download.php) or by configuring the webserver to return different headers for the file you want to download. You can do this on a per-directory basis on the webserver, which would allow you to get away without writing any php or javascript - simply have all your download images in that one location.

Colin Pickard
No. Lying about the content-type is not the only way to achieve this. See the content-disposition HTTP response header.
David Dorward
You're right, thank you. I have corrected my answer
Colin Pickard