Forum Moderators: phranque

Message Too Old, No Replies

Get requested filename into env

need it for Content-Disposition

         

Martin Strand

3:02 pm on May 20, 2007 (gmt 0)

10+ Year Member



I'm using "Content-Disposition: attachment" to force a download dialog, but I also need to add the 'filename' parameter or else an IE6 bug is triggered. Without 'filename', if you click "Open" in IE6, the file is downloaded and then immediately deleted before it is opened.

So, I need the currently requested filename in an environment varoable so I can insert it into the header:


<Location /files>
SetEnv FILENAME filename # <-- need the actual filename here!
Header set Content-Disposition "attachment; filename=%{FILENAME}e"
</Location>

Is this possible?

Martin

g1smd

4:14 pm on May 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I usually set the MIME type for that filename extension to always force a download.

I assume that it is for ZIP or other such files?

jdMorgan

8:41 pm on May 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That variable is already defined:

Header set Content-Disposition "attachment; filename=%{REQUEST_FILENAME}"

I'm not sure if mod_headers will de-reference the variable. If it doesn't, then consider writing the header using a script.

You might want to take a look through the list of cgi variables if that one does not suit your needs.

Jim

Martin Strand

3:11 am on May 21, 2007 (gmt 0)

10+ Year Member



Here's the solution I ended up with:

SetEnvIf REQUEST_URI "/([^/]+)$" FILENAME=$1
Header set Content-Disposition "attachment; filename=\"%{FILENAME}e\"" env=FILENAME

It works fine, but unfortunately it didn't solve the problem I had with IE. The problem with IE was that if you clicked "Open", it would download the file but then delete it right before launching the external application (adobe pdf in this case)
This happened because I used mod_rewrite for mass vhosts:


UseCanonicalName Off
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)*(.+)$
RewriteRule ^/(.*)$ /var/www/vhosts/%2/$1

and mod_rewrite then adds "Vary: Host" to the response. IE would then mark the file as completely uncacheable (not just "Vary: Host") and delete it immediately after download.

I found a few workarounds here:
[iosart.com...]

Now I just use SERVER_NAME instead of HTTP_HOST, which works fine if UseCanonicalName is off. This way mod_rewrite won't understand that you're actually using the "Host" request header and won't add that "Vary: Host" header:


UseCanonicalName Off
RewriteEngine On
RewriteCond %{SERVER_NAME} ^(www.)*(.+)$
RewriteRule ^/(.*)$ /var/www/vhosts/%2/$1

Another solution is to simply remove the Vary response header with mod_headers.

Martin

jdMorgan

4:07 am on May 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This gets the first-half-of-2007 award for the most difficult IE workaround!

Glad you figured it out.

Jim