2

I need to provide a button on a webpage that can optionally cause an image file to be downloaded and stored in the client's filesystem. I thought to use the approach from Force a file or image to download using .htaccess and wrote in my .htaccess, following the style of the Apache documentation,

<If "%{QUERY_STRING} =~ /download/">
Header set Content-Disposition attachment
</If>

I then provide a link like http://www.example.com/images/image123.jpeg?download=true, and that works fine with my production server. But it gives me an error 500 on the Mac development system on my desk, using either OS/X's apache or MAMP. Apache complains it does not understand the <If> statement, and serverFault tells me that is not understood by Apache 2.2. Both OS/X and MAMP provide Apache 2.2.

Is there a way I can rewrite the <if> statement to produce the same effect in Apache 2.2, please?

I can't just use

<a download="clientFileName.jpg" href="/images/image123.jpeg">Download</a> 

because it doesn't work in either IE11 or Safari.

Community
  • 1
  • 1
emrys57
  • 6,679
  • 3
  • 39
  • 49
  • Use a server side programming language. Like PHP – Dávid Szabó May 14 '14 at 13:34
  • I initially, successfully, provided this with PHP, but now image sizes are in the hundreds of megabytes. The PHP process is forcefully terminated by the hosting service for running too long. I cannot extend the allowed runtime. – emrys57 May 14 '14 at 13:36

1 Answers1

2

You can set an env variable using rewrite rule first:

RewriteEngine On

RewriteCond %{QUERY_STRING} (?:^|&)download=[^&]+ [NC]
RewriteRule ^ - [E=DL:1,L]

Then use this env variable in conditionally setting up your header:

Header set Content-Disposition attachment env=DL
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    That looks very promising, it does seem to work. Now, if only I could understand the syntax...! Give me a few minutes here. – emrys57 May 14 '14 at 13:52
  • 1
    Yes, that definitely does work, after I had read enough of the Apache documentation to understand what you were saying. Thanks very much! – emrys57 May 14 '14 at 15:30