iphone 获取文件mimeType 方法

- (NSString *)getMimeType:(NSString *)fileAbsolutePath error:(NSError *)error
{
    NSString* fullPath = [fileAbsolutePath stringByExpandingTildeInPath];
    NSURL* fileUrl = [NSURL fileURLWithPath:fullPath]; 
    NSURLRequest* fileUrlRequest = [NSURLRequest requestWithURL: fileUrl];
    NSURLResponse* response = nil;
    [NSURLConnection sendSynchronousRequest:fileUrlRequest returningResponse:&response error:&error];
    return[response MIMEType];

}

 

   // need MobileCoreServices.framework

   //#import <MobileCoreServices/MobileCoreServices.h>
   //#import <CoreFoundation/CoreFoundation.h>

   CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (CFStringRef)@"xls", NULL);
   CFStringRef MIMEType = UTTypeCopyPreferredTagWithClass (UTI, kUTTagClassMIMEType);
   CFRelease(UTI);
  
   NSString *mimeType = [(NSString *)MIMEType autorelease];
   NSLog(mimeType); // output "application/vnd.ms-excel"

//

你可能感兴趣的:(iPhone)