1

I have a gallery page that lists medium and high resolution images of plants that users can download. However, the default behavior of a jpeg image inside an href is to open in the browser window. For these specific links I would like users to be able to download the image when clicking on the link instead of having to open the image in the browser and then save it.

Here is the code I have:

<div class="topic-links">
 <div class="topic col-sm-4 col-md-4">
  <img src="{gallery_thumb}" alt="temp_square-tn" width="220" height="200">
  <h6>{exp:char_limit total="30" exact="no"}{title}{/exp:char_limit}</h6>
  <p>
    {if medium_res != ""}<a href="{medium_res}">Medium</a><br />{/if}
    {if high_res != ""}<a href="{high_res}">Large</a>{/if}
  </p>
  </a>
 </div><!-- /.topic -->
</div><!-- /.topic-links -->

What is the best way to force a download?

2
  • Think I needed to use a htaccess file to do this on a vcard previously. Server will need mod_headers installed for this to work. stackoverflow.com/questions/4218466/…
    – MBM
    Commented Aug 21, 2015 at 22:38
  • I confirmed that mod_headers is active on the server and added the code to an .htaccess file in that directory, but it is not working as desired. The image still opens in a browser window.
    – forrest
    Commented Aug 21, 2015 at 23:00

3 Answers 3

2

If using HTML5, you just need to add the download attribute

3
  • Holy crap, up-vote for you. How did I miss that??? Thanks for answering!
    – jrothafer
    Commented Sep 18, 2015 at 6:06
  • I tried this but it does not work in all situations, even with modern browsers.
    – forrest
    Commented Sep 22, 2015 at 22:32
  • chances are you missed something, caniuse is one ressource to be trusted
    – laurent
    Commented Sep 23, 2015 at 8:59
1

This is possible and the solution is quite simple:

  1. add the reference to the image in an anchor tag: Medium
  2. Add the following code to the .htaccess file:

Header set Content-Disposition attachment

Example: http://southernlivingplants.com/resources/image-gallery

0

This is not possible. The browser chooses how to handle these files, and that is client side. That's why

Right click to download

Is a thing. A user would have to configure their browser or use a third party browser addon to do this. Sorry, chief.

Edit

If you force the mime type, it's possible. But to do it selectively will require (if possible) some serious hacks, I'm betting.

https://www.timewasters-place.com/how-to-force-file-downloading-with-htaccess/

Edit on my way to a party on a train, so I'm bored, lol.

You could make a custom module that you link to, like this:

Link to address: domain.com/?ACT=123&imagepath=images/myimage.jpg

And in your module, use this answer:

https://stackoverflow.com/questions/1851849/output-an-image-in-php

And throw an exit() after. I don't know, killing time on the train! Hopefully leads you in a successful direction.

Last Edit

http://php.net/manual/en/function.imagejpeg.php

Set your header for the octet/stream header, then try that function! I don't get paid enough :p

1
  • In 2015, this may not have been possible but as of Nov 2020 it is by adding a response header of Content-Disposition: attachment; filename="cool-image.jpg" (as stated in the answer).
    – PatS
    Commented Nov 3, 2020 at 21:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.