Project

General

Profile

[Solved] Force "Content-Disposition" => "attachment", regardless of the MIME type

Added by l3u over 2 years ago

Dear forum,

how can I force a file to be offered as a download (not viewing inside the browser), regardless of the MIME type?

I have some files containing checksums for downloads. They are called some-program.tar.xz.checksums . By default, a download is offered if I click on a link pointing to them. The MIME type is application/octet-stream.

Actually, the files are plain text files. So I tried the following:

$HTTP["url"] =~ "^/opensource/.*\.checksums$" {
    mimetype.assign = ( ".checksums" => "text/plain" )
    setenv.add-response-header = ( "Content-Disposition" => "attachment" )
}

After that, the file is not provided as a download anymore, but viewed directly inside the browser. So apparently, the Content-Disposition setting is simply ignored, or the default inline-viewing for text/plain is used although I (think I) overwrite the setting. If I set mimetype.assign = ( ".checksums" => "application/octet-stream" ), the file is offered as a download again.

How can I both set the correct MIME type and have the file as a download?

Thanks for all help!


Replies (7)

RE: Force "Content-Disposition" => "attachment", regardless of the MIME type - Added by l3u over 2 years ago

Oh, I run Lighttpd 1.4.55 an an Ubuntu 20.04 server

RE: Force "Content-Disposition" => "attachment", regardless of the MIME type - Added by gstrauss over 2 years ago

how can I force a file to be offered as a download (not viewing inside the browser), regardless of the MIME type?

You are requesting information on client behavior.

Once you figure out how to trigger this behavior on the client, you'll be able to configure lighttpd to assist.
What does your search engine tell you about how to trigger you desired behavior in your web client/browser?

RE: Force "Content-Disposition" => "attachment", regardless of the MIME type - Added by gstrauss over 2 years ago

See "Examples" section in https://www.php.net/manual/en/function.header.php

You may want to specify filename=... in the Content-Disposition response here.
You can use mod_magnet instead of mod_setenv to construct the header filename=... from the targer filename.
https://redmine.lighttpd.net/boards/2/topics/3127

RE: Force "Content-Disposition" => "attachment", regardless of the MIME type - Added by l3u over 2 years ago

I moved the respective site from an Apache web server to Lighttpd. With Apache, I used the following .htaccess file:

<FilesMatch "\.checksums$">
    ForceType text/plain
    Header add Content-Disposition "attachment" 
</FilesMatch>

and this triggered the desired behavior, the file was offered as a download on all browsers I tried. I simply try to reproduce this behavior with Lighttpd …

I had a similar setting for .asc files containing signatures for release packages. The .htaccess file looked like this:

<FilesMatch "\.asc$">
    ForceType application/pgp-signature
    Header add Content-Disposition "attachment" 
</FilesMatch>

I "translated" this to:

$HTTP["url"] =~ "^/download/.*\.asc$" {
    mimetype.assign = ( ".asc" => "application/pgp-signature" )
    setenv.add-response-header = ( "Content-Disposition" => "attachment" )
}

and it works like before. Thus I didn't think this was a client-side problem …

RE: Force "Content-Disposition" => "attachment", regardless of the MIME type - Added by l3u over 2 years ago

gstrauss wrote in RE: Force "Content-Disposition" => "attachment", regardle...:

See "Examples" section in https://www.php.net/manual/en/function.header.php

PHP is not involved here, I just want to serve a plain text file, but tell the browser not to show it, but to download it

RE: Force "Content-Disposition" => "attachment", regardless of the MIME type - Added by l3u over 2 years ago

Okay, I'm wearing sackcloth and ashes. I simply forgot to add mod_setenv. So the set Content-Disposition header wasn't added in the response … sorry for the spam ;-)

RE: Force "Content-Disposition" => "attachment", regardless of the MIME type - Added by gstrauss over 2 years ago

Glad you figured it out. Please run lighttpd -f /etc/lighttpd/lighttpd.conf -tt to test your config (and correct any warnings) after you make any changes.

    (1-7/7)