如何让我的Apache 2服务器强制浏览器打开文件传输对话框?

时间:2021-09-15 03:48:24

How do I make my Apache 2 server force a browser to open a file transfer dialogue if the URL points to a file with a .pln or .psa extension?

如果URL指向扩展名为.pln或.psa的文件,如何让我的Apache 2服务器强制浏览器打开文件传输对话框?

I have a simple LAMP server with CentOS 5, Apache 2, MYSQL 5, PHP 5, recently built CentOS 5.2 i386 installation CDs. My web application generates files to be downloaded and imported into a custom application. The file extensions are .psa and .pln. How do I make my server force the browser to open a file transfer dialogue? If I point my browser to a .psa or .pln file on the Apache 2 server, the file's content is displayed in a pop-up window as simple text. I want a file transfer dialogue.

我有一个简单的LAMP服务器,包括CentOS 5,Apache 2,MYSQL 5,PHP 5,最近构建的CentOS 5.2 i386安装CD。我的Web应用程序生成要下载的文件并导入到自定义应用程序中。文件扩展名为.psa和.pln。如何让我的服务器强制浏览器打开文件传输对话框?如果我将浏览器指向Apache 2服务器上的.psa或.pln文件,则文件的内容将作为简单文本显示在弹出窗口中。我想要一个文件传输对话。

The web-app I am working on is deployed on another web-server and handles the .pln and .psa files as desired. I cannot compare server configuration files because I do not have administrator access to the working server.

我正在处理的Web应用程序部署在另一个Web服务器上,并根据需要处理.pln和.psa文件。我无法比较服务器配置文件,因为我没有对工作服务器的管理员访问权限。

How do I change my server's behavior? Does this require code changes to my web-app code (such as sending explicit headers)? If so, why does it work against the other server? Can code changes be avoided by configuring the server's default behavior?

如何更改服务器的行为?这是否需要更改我的网络应用程序代码(例如发送显式标头)?如果是这样,为什么它对其他服务器起作用?通过配置服务器的默认行为可以避免代码更改吗?

Any help would be appreciated.

任何帮助,将不胜感激。

5 个解决方案

#1


You should be able to use the FilesMatch directive to add the necessary header.

您应该能够使用FilesMatch指令添加必要的标头。

<FilesMatch "\.(?i:pin)$">
  Header set Content-Disposition attachment
</FilesMatch>

#2


Forcing a browser to do something is always a tricky proposition, since the browser can ignore you and do what the hell it likes :)

强制浏览器做某事总是一个棘手的主张,因为浏览器可以忽略你并做它喜欢的东西:)

That said, most browsers will prompt the user with a "save as" dialog box if the "Content-type" header is set to "application/octet-stream". Either write a simple wrapper cgi that serves up the requested file with the correct header, or fiddle with Apache2's mime-types (look in the config directory.)

也就是说,如果“Content-type”标题设置为“application / octet-stream”,大多数浏览器将提示用户使用“另存为”对话框。编写一个简单的包装器cgi,使用正确的标头提供所请求的文件,或者使用Apache2的mime-types(查看config目录)。

Hope this helps, happy hacking (and happy Christmas, New Year or whatever you may celebrate)

希望这会有所帮助,快乐的黑客行为(以及圣诞快乐,新年或任何你可能庆祝的事情)

Misha

#3


Ahh, didn't know about that header palehorse, that's probably a better way, since it allows you to preserve the original MIME type, will have to remember that one.

啊,不知道那个标题苍白,这可能是一个更好的方法,因为它允许你保留原始的MIME类型,将不得不记住那一个。

#4


I tried several configuration changes which had no apparent effect.

我尝试了几个没有明显效果的配置更改。

  1. I added the following line to my /etc/httpd/conf/httpd.conf file:
  2. 我在/etc/httpd/conf/httpd.conf文件中添加了以下行:

AddType application/octet-stream .pln .psa

AddType application / octet-stream .pln .psa

I restarted the Apache server and it had no effect.

我重新启动了Apache服务器,它没有任何效果。

  1. I added the following lines to my /etc/httpd/conf/httpd.conf file: ForceType application/octet-stream Header set Content-Disposition attachment ForceType application/octet-stream Header set Content-Disposition attachment I restarted the Apache server and it had no effect.
  2. 我在/etc/httpd/conf/httpd.conf文件中添加了以下行:ForceType application / octet-stream Header set Content-Disposition attachment ForceType application / octet-stream Header set Content-Disposition attachment我重新启动了Apache服务器并且它没有效果。

#5


If you have Firefox (and if not, why not?) install Chris Pedericks Developer Toolbar, and check that the headers are actually being set correctly. If so, it may be the fault of the browser. As I said, you can't be certain that any given browser will "correctly" interpret the response headers. What browser are we talking about here anyway?

如果您有Firefox(如果没有,为什么不呢?)安装Chris Pedericks开发人员工具栏,并检查标题是否实际设置正确。如果是这样,可能是浏览器的错误。正如我所说,你不能确定任何给定的浏览器将“正确”解释响应头。我们在这里谈论什么浏览器?

If the headers aren't being set correctly, you may need to re-check your httpd.conf file. Possibly the directives you added aren't in the correct section? (e.g. under the wrong <Location > directive)

如果未正确设置标头,则可能需要重新检查httpd.conf文件。可能你添加的指令不在正确的部分? (例如,在错误的 指令下)

#1


You should be able to use the FilesMatch directive to add the necessary header.

您应该能够使用FilesMatch指令添加必要的标头。

<FilesMatch "\.(?i:pin)$">
  Header set Content-Disposition attachment
</FilesMatch>

#2


Forcing a browser to do something is always a tricky proposition, since the browser can ignore you and do what the hell it likes :)

强制浏览器做某事总是一个棘手的主张,因为浏览器可以忽略你并做它喜欢的东西:)

That said, most browsers will prompt the user with a "save as" dialog box if the "Content-type" header is set to "application/octet-stream". Either write a simple wrapper cgi that serves up the requested file with the correct header, or fiddle with Apache2's mime-types (look in the config directory.)

也就是说,如果“Content-type”标题设置为“application / octet-stream”,大多数浏览器将提示用户使用“另存为”对话框。编写一个简单的包装器cgi,使用正确的标头提供所请求的文件,或者使用Apache2的mime-types(查看config目录)。

Hope this helps, happy hacking (and happy Christmas, New Year or whatever you may celebrate)

希望这会有所帮助,快乐的黑客行为(以及圣诞快乐,新年或任何你可能庆祝的事情)

Misha

#3


Ahh, didn't know about that header palehorse, that's probably a better way, since it allows you to preserve the original MIME type, will have to remember that one.

啊,不知道那个标题苍白,这可能是一个更好的方法,因为它允许你保留原始的MIME类型,将不得不记住那一个。

#4


I tried several configuration changes which had no apparent effect.

我尝试了几个没有明显效果的配置更改。

  1. I added the following line to my /etc/httpd/conf/httpd.conf file:
  2. 我在/etc/httpd/conf/httpd.conf文件中添加了以下行:

AddType application/octet-stream .pln .psa

AddType application / octet-stream .pln .psa

I restarted the Apache server and it had no effect.

我重新启动了Apache服务器,它没有任何效果。

  1. I added the following lines to my /etc/httpd/conf/httpd.conf file: ForceType application/octet-stream Header set Content-Disposition attachment ForceType application/octet-stream Header set Content-Disposition attachment I restarted the Apache server and it had no effect.
  2. 我在/etc/httpd/conf/httpd.conf文件中添加了以下行:ForceType application / octet-stream Header set Content-Disposition attachment ForceType application / octet-stream Header set Content-Disposition attachment我重新启动了Apache服务器并且它没有效果。

#5


If you have Firefox (and if not, why not?) install Chris Pedericks Developer Toolbar, and check that the headers are actually being set correctly. If so, it may be the fault of the browser. As I said, you can't be certain that any given browser will "correctly" interpret the response headers. What browser are we talking about here anyway?

如果您有Firefox(如果没有,为什么不呢?)安装Chris Pedericks开发人员工具栏,并检查标题是否实际设置正确。如果是这样,可能是浏览器的错误。正如我所说,你不能确定任何给定的浏览器将“正确”解释响应头。我们在这里谈论什么浏览器?

If the headers aren't being set correctly, you may need to re-check your httpd.conf file. Possibly the directives you added aren't in the correct section? (e.g. under the wrong <Location > directive)

如果未正确设置标头,则可能需要重新检查httpd.conf文件。可能你添加的指令不在正确的部分? (例如,在错误的 指令下)