使用htaccess在标头中设置文件名 [英] Setting a filename inside the header with htaccess

查看:127
本文介绍了使用htaccess在标头中设置文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个应用程序,可以从服务器下载文件。下载它们后,需要保存它,因为它使用了响应头中的文件名参数。

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 the responce header.

当我使用标准PHP <$时c $ c> download.php?id = downloadID 我可以毫无问题地设置标题。

When i'm using the standart PHP download.php?id=downloadID i can set the headers with no problem.

现在的任务是在客户端上服务器上没有可用的php,
i需要在响应头中设置文件名。

The task now is that on the client server they do not have any php available, i need to set the filename inside of the responce header.

这是我拥有的htaccess:

here is the htaccess what i have:

<FilesMatch "\.(?i:pdf)$">
    Header set Content-Type: application/octet-stream
    Header set Content-Disposition: "attachment; filename=FILE_NAME.pdf"
</FilesMatch>

我不知道如何使用htaccess将文件名动态地解析为标题。
我已经在Google上搜索了它,但仍然没有解决方案。

And i have no idea how to parce the file name dynamicaly to the header using htaccess. I've google it looked for it but still no solution.

有什么建议吗?

推荐答案

我不知道它是否有效,但这是我的第一次尝试。

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>

编辑似乎某些apache安装会在env变量前加上 REDIRECT _

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.

但是,如果请求URL也包含文件名,为什么还要在标题中设置文件名呢?所有浏览器都将在URL中的名称下保存文件。因此,转到 /path/test.pdf 将导致浏览器建议文件名 test.pdf

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.

这篇关于使用htaccess在标头中设置文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆