获得文件的MIMEType
在开发中,我们有些时候需要获得文件的MIMEType,而好多人却苦于不知道如何获取,这里将提供四种方法:
* 01 发送请求|得到响应头信息(MIMEType)
* 02 直接百度查表 http://www.w3school.com.cn/media/media_mimeref.asp
* 03 使用C语言的函数来获取
* 04 设置为通用的二进制数据类型 application/octet-stream
c语言方法:
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
NSLog(@"%@",[self mimeTypeForFileAtPath:@"/Users/xiaomage/Desktop/Snip20160716_103.png"]);
}
- (NSString *)mimeTypeForFileAtPath:(NSString *)path
{
if (![[[NSFileManager alloc] init] fileExistsAtPath:path]) {
return nil;
}
CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)[path pathExtension], NULL);
CFStringRef MIMEType = UTTypeCopyPreferredTagWithClass (UTI, kUTTagClassMIMEType);
CFRelease(UTI);
if (!MIMEType) {
return @"application/octet-stream";
}
return (__bridge NSString *)(MIMEType);
}
发送请求|得到响应头信息(MIMEType)
-(void)getMimetype
{
[[[NSURLSession sharedSession] dataTaskWithURL:[NSURL fileURLWithPath:@"/Users/xiaomage/Desktop/上课笔记.h"] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//MIMEType = 大类型/小类型
NSLog(@"%@",response.MIMEType);
}] resume];
}