如何Find MIME Types
Look 上 MIME (内容) types 为 file extensions 和 vice versa.
问题
你需要集合正确 Content-类型头 为 file 或确定什么类型 file 是从 其扩展.
前置条件
- Understanding 的 HTTP headers
- Knowledge 的 file types
步骤
-
1. Look up by file extension
Map the file extension to its MIME type: .json → application/json, .html → text/html, .png → image/png, .pdf → application/pdf. Use a comprehensive MIME type database for accurate mapping, as some extensions have multiple registered types.
输入.json输出application/json -
2. Handle ambiguous extensions
Some extensions map to multiple MIME types depending on context: .xml can be text/xml or application/xml, .js can be text/javascript or application/javascript. Use the most widely compatible type. For web serving, follow the IANA registry.
输入.xml输出application/xml (preferred) -
3. Set the Content-Type correctly
When serving files, set the Content-Type header to the correct MIME type. Browsers use this to determine how to handle the response. An incorrect type can cause a file to download instead of display, or render incorrectly. Add charset for text types: text/html; charset=utf-8.
输入serving a JSON file输出Content-Type: application/json; charset=utf-8
预期结果
正确 MIME 类型为 file 扩展, 或正确扩展为 MIME 类型.
常见错误
- Wrong Content-Type causes file to download
- Set the correct MIME type. For example, text/html for HTML, application/json for JSON. A wrong type like application/octet-stream causes download.
- Missing charset for text types
- Add charset=utf-8 to text MIME types (text/html, text/plain, application/json). Without it, the browser may guess the encoding incorrectly.
排障指引
- If a file renders as plain text, the Content-Type is likely text/plain instead of the correct type.
- For custom file types, use application/octet-stream as a fallback.
安全限制
- MIME type sniffing can be a security risk. Set X-Content-Type-Options: nosniff to prevent browsers from guessing the type.