如何在objective-c中读取MIME类型的文件 [英] How can you read a files MIME-type in objective-c

查看:131
本文介绍了如何在objective-c中读取MIME类型的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有兴趣检测iPhone应用程序文档目录中文件的MIME类型。通过文档搜索没有提供任何答案。

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.

推荐答案

这有点hacky,但它应该工作,不知道当然,因为我只是猜测它

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

有两种选择:


  1. 如果您只需要MIME类型,请使用timeoutInterval:NSURLRequest。

  2. 如果您还想要数据,则应使用注释掉的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];

这篇关于如何在objective-c中读取MIME类型的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆