使用htaccess在标头中设置文件名

编程入门 行业动态 更新时间:2024-10-23 02:00:21
本文介绍了使用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 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在标头中设置文件名

本文发布于:2023-11-01 17:56:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1550012.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件名   htaccess   标头中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!