美文网首页
获取文件MIMEType

获取文件MIMEType

作者: Coder007 | 来源:发表于2017-04-01 10:46 被阅读45次
    • 获取文件的MIMEType有两种方式:
    #import "NSString+MIMEType.h"
    #import <MobileCoreServices/MobileCoreServices.h>
    
    @implementation NSString (MIMEType)
    - (NSString *)MIMEType
    {
        if (![[NSFileManager defaultManager] fileExistsAtPath:self]) {
            return nil;
        }
        CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge  CFStringRef)[self pathExtension], NULL);
        CFStringRef MIMEType = UTTypeCopyPreferredTagWithClass (UTI, kUTTagClassMIMEType);
        CFRelease(UTI);
        if (!MIMEType) {
            return @"application/octet-stream";
        }
        return (__bridge NSString *)MIMEType;
    }
    
    - (NSString *)MIMEType2
    {
        NSURLResponse *response = nil;
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:self]];
        [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
        return response.MIMEType;
    }
    @end
    

    相关文章

      网友评论

          本文标题:获取文件MIMEType

          本文链接:https://www.haomeiwen.com/subject/wchiottx.html