Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you read a files MIME-type in objective-c

I am interested in detecting the MIME-type for a file in the documents directory of my iPhone application. A search through the docs did not provide any answers.

like image 895
coneybeare Avatar asked Sep 01 '09 18:09

coneybeare


People also ask

Where is MIME type stored in a file?

All MIME type information is stored in a database. The MIME database is located in the directory /usr/share/mime/ . The MIME database contains a large number of common MIME types, stored in the file /usr/share/mime/packages/freedesktop.

How do I specify a MIME type?

A MIME type has two parts: a type and a subtype. They are separated by a slash (/). For example, the MIME type for Microsoft Word files is application and the subtype is msword. Together, the complete MIME type is application/msword.


1 Answers

It's a bit hacky, but it should work, don't know for sure because I'm just guessing at it

There are two options:

  1. If you just need the MIME type, use the timeoutInterval: NSURLRequest.
  2. If you want the data as well, you should use the commented out NSURLRequest.

Make sure to perform the request in a thread though, since it's synchronous.

NSString* filePath = [[NSBundle mainBundle] pathForResource:@"imagename" ofType:@"jpg"]; NSString* fullPath = [filePath stringByExpandingTildeInPath]; NSURL* fileUrl = [NSURL fileURLWithPath:fullPath]; //NSURLRequest* fileUrlRequest = [[NSURLRequest alloc] initWithURL:fileUrl]; NSURLRequest* fileUrlRequest = [[NSURLRequest alloc] initWithURL:fileUrl cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:.1];  NSError* error = nil; NSURLResponse* response = nil; NSData* fileData = [NSURLConnection sendSynchronousRequest:fileUrlRequest returningResponse:&response error:&error];  fileData; // Ignore this if you're using the timeoutInterval           // request, since the data will be truncated.  NSString* mimeType = [response MIMEType];  [fileUrlRequest release]; 
like image 127
slf Avatar answered Oct 08 '22 01:10

slf