iOS開發之網路編程--擷取檔案的MIMEType,iosmimetype

來源:互聯網
上載者:User

iOS開發之網路編程--擷取檔案的MIMEType,iosmimetype

前言:有時候我們需要擷取檔案的MIMEType的資訊,下面就介紹關於擷取MIMEType的方法。

 

1、直接百度搜尋索引鍵"MIMEType",你會找到,然後查吧:

2、用代碼擷取檔案的MIMEType資訊:

 1 #import "GetMIMEType.h" 2  3 #import <MobileCoreServices/MobileCoreServices.h> 4  5 @implementation GetMIMEType 6  7 #pragma mark - 類方法 8 + (NSString*)getMIMETypeURLRequestAtFilePath:(NSString*)path{ 9     return [[[GetMIMEType alloc] init] getMIMETypeURLRequestAtPath:path];10 }11 12 + (NSString *)getMIMETypeWithCAPIAtFilePath:(NSString *)path{13     return [[[GetMIMEType alloc] init] getMIMETypeWithCAPIAtFilePath:path];14 }15 #pragma mark - 對象方法16 //向該檔案發送請求,根據要求標頭拿到該檔案的MIMEType17 -(NSString *)getMIMETypeURLRequestAtPath:(NSString*)path18 {19     //1.確定請求路徑20     NSURL *url = [NSURL fileURLWithPath:path];21     22     //2.建立可變的請求對象23     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];24     25     //3.發送請求26     NSHTTPURLResponse *response = nil;27     [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];28     29     NSString *mimeType = response.MIMEType;30     return mimeType;31 }32 33 //調用C語言的API來獲得檔案的MIMEType ,只能擷取本地檔案哦,無法擷取網路請求來的檔案34 -(NSString *)getMIMETypeWithCAPIAtFilePath:(NSString *)path35 {36     if (![[[NSFileManager alloc] init] fileExistsAtPath:path]) {37         return nil;38     }39     40     CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)[path pathExtension], NULL);41     CFStringRef MIMEType = UTTypeCopyPreferredTagWithClass (UTI, kUTTagClassMIMEType);42     CFRelease(UTI);43     if (!MIMEType) {44         return @"application/octet-stream";45     }46     return (__bridge NSString *)(MIMEType)47     ;48 }49 50 @end

運行:

github源碼下載:https://github.com/HeYang123456789/GetMIMEType

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.