简体   繁体   English

如何在HTTP响应中选择Content-Type

[英]How to choose Content-Type in HTTP response

I write small http server. 我写小http服务器。 I need to set Content-Type when I send file in response. 当我发送文件作为响应时,我需要设置Content-Type。 How it should be done? 应该怎么做? Should Content-Type field be preset in some map for each file or it might be set basing on file extension? 是否应在某个地图中为每个文件预设Content-Type字段,或者可以根据文件扩展名设置它? If yes, how about *.ico files that are PNG actually? 如果是,那么实际上是PNG *.ico文件呢?

So what's the common practice? 那么常见的做法是什么?

So what's the common practice? 那么常见的做法是什么?

Common practice, for static files, is to infer the Content-Type from the file extension. 对于静态文件,通常的做法是从文件扩展名推断Content-Type。

If yes, how about *.ico files that are PNG actually? 如果是,那么实际上是PNG的* .ico文件呢?

Then common practice would give the wrong content-type. 然后,通常的做法将给出错误的内容类型。

You could override it on a case-by-case basis from a configuration file. 您可以根据具体情况从配置文件中覆盖它。

eg Apache HTTPD would allow: 例如Apache HTTPD将允许:

<Files "favicon.ico">
    ForceType image/png
</Files>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM