Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can I get Mime Type in IOS which is not based on extension

we know that we can get the file's MIME TYPE from the file's extension,but it's not exactly.for example we changed the file's extension and we will get the wrong mime type. and also we know the way that get the mime type by a file signature in C#,(using the urlmon.dll Using .NET, how can you find the mime type of a file based on the file signature not the extension ,my question is that how can we get the exact mime type in IOS,no matter the file's extension is changed by someone,we can getthe right mime type.

thank you for your attention~!

like image 726
HamasN Avatar asked Mar 21 '12 08:03

HamasN


People also ask

How do I add a MIME type?

In the Connections pane, go to the site, application, or directory for which you want to add a MIME type. In the Home pane, double-click MIME Types. In the MIME Types pane, click Add... in the Actions pane. In the Add MIME Type dialog box, add the file name extension and MIME type, and then click OK.

How I can get the MIME type of a file having its URI?

To get the data type of a shared file given its content URI, the client app calls ContentResolver. getType() . This method returns the file's MIME type. By default, a FileProvider determines the file's MIME type from its filename extension.


1 Answers

Would you tell me how I get the right mime type exactly when I get a path of a file.

iOS uses the concept of Uniform Type Identifiers (UTI) to handle document types.

NSString *path; // contains the file path

// Get the UTI from the file's extension:

CFStringRef pathExtension = (__bridge_retained CFStringRef)[path pathExtension];
CFStringRef type = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, pathExtension, NULL);
CFRelease(pathExtension);

// The UTI can be converted to a mime type:

NSString *mimeType = (__bridge_transfer NSString *)UTTypeCopyPreferredTagWithClass(type, kUTTagClassMIMEType);
if (type != NULL)
    CFRelease(type);

You should consider using UTIs for your purpose directly instead of converting them to the less powerful mime type.

like image 151
Nikolai Ruhe Avatar answered Nov 15 '22 22:11

Nikolai Ruhe