我试图从扩展中获取Mime类型.html应该回馈text/html.我知道如何使用文件获取Mime但不是其他方式.有没有办法从扩展中查询mime,至少是已知的?
你需要GContentType在GIO里面使用:
https://developer.gnome.org/gio/stable/gio-GContentType.html
确切地说g_content_type_guess():
https://developer.gnome.org/gio/stable/gio-GContentType.html#g-content-type-guess
它接收文件名或文件内容,并返回猜测的内容类型; 从那里,您可以使用g_content_type_get_mime_type()获取内容类型的MIME类型.
这是一个如何使用的示例g_content_type_guess():
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <gio/gio.h>
int
main (int argc, char *argv[])
{
const char *file_name = "test.html";
gboolean is_certain = FALSE;
char *content_type = g_content_type_guess (file_name, NULL, 0, &is_certain);
if (content_type != NULL)
{
char *mime_type = g_content_type_get_mime_type (content_type);
g_print ("Content type for file '%s': %s (certain: %s)\n"
"MIME type for content type: %s\n",
file_name,
content_type,
is_certain ? "yes" : "no",
mime_type);
g_free (mime_type);
}
g_free (content_type);
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
编译完成后,Linux上的输出是:
Content type for file 'test.html': text/html (certain: no)
MIME type for content type: text/html
Run Code Online (Sandbox Code Playgroud)
解释:在Linux上,内容类型是MIME类型; 在其他平台上不是这样,这就是为什么必须将GContentType字符串转换为MIME类型.
另外,正如您所看到的,仅使用扩展名将布尔值设置为特定标志,因为扩展本身不足以确定准确的内容类型.
| 归档时间: |
|
| 查看次数: |
2420 次 |
| 最近记录: |