我正在添加从网络服务器下载文件的功能。我正在使用自己的 Google 云端硬盘帐户测试我的程序,但我不确定应该如何处理 Content-Type header ,因为我对网络编程了解不多。
首先,我将两个文件上传到我的 Google 云端硬盘帐户。 两者都是完全相同的 zip 文件,但我将其中一个的扩展名从“zip”重命名为“ktx”,这在上传测试之前没有任何意义。 上传后,我检查了它们的 Content-Type header ,原始 header 为“application/zip”,重命名 header 为“application/x-zip”。
我很困惑,所以搜索了 Content-Type header ,但变得更加困惑。因为 zip 似乎有更多名称,例如“multipart/x-zip”和“application/x-zip-compressed”。 所以我真的很想知道 Content-Type header 是否有标准命名约定。
无论如何,我上传了我需要的所有文件类型,下面是我从 Google 云端硬盘获得的文件类型。
Documents
text/plain (txt)
application/pdf
application/msword (doc)
application/vnd.ms-powerpoint (ppt)
application/vnd.ms-excel (xls)
application/rtf
application/vnd.openxmlformats-officedocument.wordprocessingml.document (docx)
application/vnd.openxmlformats-officedocument.presentationml.presentation (pptx)
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet (xlsx)
Images
image/jpeg
image/png
image/gif
image/tiff (tif, tiff)
image/x-ms-bmp
image/x-photoshop (psd)
image/x-raw (raw)
application/illustrator (ai)
Compressed
application/zip
application/rar
application/x-tar (tar)
application/x-gzip (gz)
application/x-7z-compressed (7z)
application/x-zip (When zip, jar, xlsx or apk is renamed to meaningless one.)
Audio files
audio/mpeg (mp3)
audio/x-wav
audio/x-ms-wma
audio/x-aiff (aiff)
audio/midi (mid)
application/ogg
Videos
video/mp4
video/x-msvideo (avi)
video/x-ms-asf
video/flv
video/quicktime (mov)
video/mpeg (mpg, vob)
video/x-ms-wmv
video/x-matroska (mkv)
Script or Source code files
text/html (htm, html)
text/x-csrc
text/x-c++src
text/x-csharp
text/x-java
text/x-python
text/css
application/x-javascript (js)
application/x-httpd-php
application/json
application/xml
Compiled or Executables
application/x-msdos-program (exe, dll, com, bat)
application/java-vm (class)
application/java-archive (jar)
application/x-python-code (pyc)
application/vnd.android.package-archive (apk)
Etc
application/x-shockwave-flash (swf)
有标准的命名约定吗?
仅使用上面的名称来检查 MIME 类型可以吗?
最佳答案
Is there any standard naming convention?
Is it okay just to use the names above to check the MIME type?
有关标准类型,请参阅 Iana Media Types .
有关 MIME 类型,请参阅 Iana MIME Media Type Parameters .
[RFC2046] specifies that Media Types (formerly known as MIME types) and Media Subtypes will be assigned and listed by the IANA.
The file types are determined once when uploading or the server always checks when downloading?
通常,当请求文件时,服务器将检查文件扩展名并确定要使用的媒体类型。然而,某些服务器可能会检查文件的第一个字节 ( magic number ) 以确定文件类型是什么(例如,zip 文件以 PK.. 开头 - 点为 0x03 0x04)。
这可能是您看到 .zip
的 "application/zip"
和压缩的 "application/x-zip"
的原因.ktx
。附带说明:.xlsx
也是一个压缩文件。
根据实现的不同,服务器可能会在接收文件时检查一次,并将其存储以供以后使用,或者在每次下载时检查一次。
关于c# - 内容类型 header 的标准命名约定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34682455/