由于各种原因,我使用NSURLProtocol拦截http请求,并从我的应用程序的文档目录中的文件加载内容。该过程的一部分涉及加载NSData对象,该对象可以是从html文件到jpeg图像的任何东西。NSURLProtocol需要设置mimetype类型。
iPhone-SDK中是否有API来确定文件或NSData内容的mimetype?
发布于 2011-05-14 07:17:55
也许您可以下载该文件并使用this获取该文件的MIME类型。
+ (NSString*) mimeTypeForFileAtPath: (NSString *) path {
if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
return nil;
}
// Borrowed from https://stackoverflow.com/questions/5996797/determine-mime-type-of-nsdata-loaded-from-a-file
// itself, derived from https://stackoverflow.com/questions/2439020/wheres-the-iphone-mime-type-database
CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (CFStringRef)[path pathExtension], NULL);
CFStringRef mimeType = UTTypeCopyPreferredTagWithClass (UTI, kUTTagClassMIMEType);
CFRelease(UTI);
if (!mimeType) {
return @"application/octet-stream";
}
return [NSMakeCollectable((NSString *)mimeType) autorelease];
}
发布于 2011-05-14 06:45:19
您是否在HTTP accept标头中看不到任何内容?如果您正在截取请求,我希望accept标头在大多数情况下都设置为预期类型的mime
https://stackoverflow.com/questions/5996797
复制相似问题