如何在不基于扩展的 IOS 中获取 Mime 类型 [英] How Can I get Mime Type in IOS which is not based on extension

查看:13
本文介绍了如何在不基于扩展的 IOS 中获取 Mime 类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们知道我们可以从文件的扩展名中获取文件的MIME TYPE,但并不完全正确.例如,我们更改了文件的扩展名,我们会得到错误的mime类型.并且我们还知道通过 C# 中的文件签名获取 mime 类型的方式,(使用 urlmon.dll 使用.NET,你怎么能根据文件签名而不是扩展名,我的问题是如何在IOS中获取准确的mime类型,无论文件的扩展名是否被某人更改,我们都可以获得正确的mime类型.

we know that we can get the file's MIME TYPE from the file's extension,but it's not exactly.for example we changed the file's extension and we will get the wrong mime type. and also we know the way that get the mime type by a file signature in C#,(using the urlmon.dll Using .NET, how can you find the mime type of a file based on the file signature not the extension ,my question is that how can we get the exact mime type in IOS,no matter the file's extension is changed by someone,we can getthe right mime type.

感谢您的关注~!

推荐答案

你能告诉我当我得到一个正确的 mime 类型时我是如何得到正确的吗?文件路径.

Would you tell me how I get the right mime type exactly when I get a path of a file.

iOS 使用了 统一类型标识符 (UTI) 用于处理文档类型.

iOS uses the concept of Uniform Type Identifiers (UTI) to handle document types.

NSString *path; // contains the file path

// Get the UTI from the file's extension:

CFStringRef pathExtension = (__bridge_retained CFStringRef)[path pathExtension];
CFStringRef type = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, pathExtension, NULL);
CFRelease(pathExtension);

// The UTI can be converted to a mime type:

NSString *mimeType = (__bridge_transfer NSString *)UTTypeCopyPreferredTagWithClass(type, kUTTagClassMIMEType);
if (type != NULL)
    CFRelease(type);

您应该考虑直接将 UTI 用于您的目的,而不是将它们转换为功能较弱的 mime 类型.

You should consider using UTIs for your purpose directly instead of converting them to the less powerful mime type.

这篇关于如何在不基于扩展的 IOS 中获取 Mime 类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆