How to get MIMETYPE of local file?

I download some files, such as word, ppt, excel. But i don’t know their MIMETYPE and suffix. Is there some way can get MIMETYPE of these file?

Follow the steps:

  • Get the file url in the directory ,e,g /Users/user/Projects/thefile.ext
  • NSString *theFileName = [url lastPathComponent]; The lastPathComponent call will return thefile.ext

  • NSString *myExtension = [theFileName stringByDeletingPathExtension] ; The stringByDeletingPathExtension call will return thefile

  • NSString *myExtension = [theFileName pathExtension] ; The pathExtension call will return ext
  • Now you get the filename and suffix follow the link : How can you read a files MIME-type in objective-c

If you don’t know the suffix, you’re forced to look at the file contents. Typically this starts with looking for magic bytes, the first few bytes of the file. You can often qualify the type of file on that basis (though you obviously can’t be sure unless you validate the whole file).

For modern Office documents, they should conform to OOXML and the first two bytes should be 0x50 0x4b (i.e. “PK”) the indicator of a zip file.

You can then uncompress it (e.g. with ZipArchive).

You can then either parse the docProps/app.xml or see Office Open XML site with links at the top of the page for how to parse word processing, excel, and presentations, respectively.