Setting a filename inside the header with htaccess

前端 未结 1 711
旧时难觅i
旧时难觅i 2021-01-17 01:08

we have an application which downloads the files from the server. After the download them it needs to save it, for that it uses the filename paramter which is comming from t

相关标签:
1条回答
  • 2021-01-17 01:52

    I have no idea if it works, but this would be my first attempt.

    RewriteRule [^/]+\.pdf$ - [E=FILENAME:$0]
    <FilesMatch "\.(?i:pdf)$">
        Header set Content-Type application/octet-stream
        Header set Content-Disposition "attachment; filename=%{FILENAME}e" 
    </FilesMatch>
    

    EDIT Seems some apache installs prefix the env variable with REDIRECT_

    RewriteRule [^/]+\.pdf$ - [E=FILENAME:$0]
    Header set Content-Type application/octet-stream env=REDIRECT_FILENAME
    Header set Content-Disposition "attachment; filename=%{REDIRECT_FILENAME}e" env=REDIRECT_FILENAME
    

    Above code doesn't work out of the box. In my case, I needed another internal redirect.

    But why would you need to set the filename in the header if the request URL contains the filename too? All browsers will save the file with under the name in the URL. So going to /path/test.pdf will result in browsers suggesting the filename test.pdf.

    0 讨论(0)
提交回复
热议问题