zoukankan      html  css  js  c++  java
  • 获得文件的MIMEType

    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
    {
    //    NSString *str = [self getMIMEType];
    //    NSLog(@"%@",str);
    //
        NSString *str = [self mimeTypeForFileAtPath:@"/Users/xmg/Desktop/122.mp4"];
        NSLog(@"%@",str);
        
    }
    
    //向该文件发送请求,根据请求头拿到该文件的MIMEType
    -(NSString *)getMIMEType
    {
        //1.确定请求路径
        NSURL *url = [NSURL fileURLWithPath:@"/Users/xmg/Desktop/122.mp4"];
        
        //2.创建可变的请求对象
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
        
        //3.发送请求
        NSHTTPURLResponse *response = nil;
        [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
        
        NSString *mimeType = response.MIMEType;
        return mimeType;
    }
    
    //调用C语言的API来获得文件的MIMEType
    -(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)
        ;
    }
    
    //可以直接百度
    

    注意:用C语言API获得文件的MIMEType要导入系统库

  • 相关阅读:
    nop调试-区域路由问题
    nop4.3 用户权限管理
    nop4.3 admin中添加新菜单
    nop 中创建任务(Task)
    SignalR 的应用
    mvc和ef如何连接
    .net中微信、支付宝回调
    C# 中使用Aspose.Words下载文档
    添加 Azure Active Directory 服务,调用方法
    Net Core 5.0 部署IIS错误-500.31-Failed to load ASP.NET Core runtime
  • 原文地址:https://www.cnblogs.com/xzk-it/p/6591975.html
Copyright © 2011-2022 走看看