识别没有扩展名的文件的扩展名 [英] Identify extension of a file without extension

查看:183
本文介绍了识别没有扩展名的文件的扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我目前的应用程序中,我从服务器下载少量文件,并将它们存储在本地。



根据文件的扩展名,我将在相应的视图中加载文件。



示例:


  1. 文件扩展名:mov,3gp,m4a等,=>在媒体播放器中显示。

  2. 文件扩展名:

  3. 文件扩展名:gif,png,xbm等,=>在UIImageView中显示
  4. b $ b

问题是:某些下载的文件可能没有任何扩展名。



我发现是在connectionDidFinishLoading从响应和获得文件扩展名获得MIMEType。我使用下面的代码,相同:

   - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
CFStringRef mimeType =(__bridge CFStringRef)[_ respo MIMEType];
CFStringRef uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType,mimeType,NULL);
CFStringRef extension = UTTypeCopyPreferredTagWithClass(uti,kUTTagClassFilenameExtension);
NSString * fileName = [NSString stringWithFormat:@%@。%@,[[_respo suggestFilename] stringByDeletingPathExtension],(__bridge NSString *)extension];
[[NSFileManager defaultManager] createFileAtPath:[[self docsDir] stringByAppendingPathComponent:[NSString stringWithFormat:@Downloads /%@,fileName]] content:_downloadedData attributes:nil];
}



现在我的问题是:


  1. 有没有其他方法来识别文件扩展名,没有扩展名,例如来自NSData对象?


  2. p>


请以任何其他方式显示档案。建议。

解决方案

另一种方法 - 你不能简单地看看你正在使用的连接的Content-下载?如果服务器知道mime类型,这似乎是一个很好的解决方案。 (如果服务器不知道mime类型,但你有它的控制,请尝试安装: http://httpd.apache.org/docs/2.2/mod/mod_mime_magic.html 或同等作品。)


In my current application I am downloading few files from server, and storing them locally.

Based on extension of file I am loading the file in appropriate view.

Example:

  1. file extensions: mov, 3gp, m4a, etc., => display in media player.
  2. file extensions: rtf, pptx, numbers, etc., =>display in UIWebView.
  3. file extensions: gif, png, xbm, etc., =>display in UIImageView.

Problem is: some of the downloaded files may not have any extension.

One approach which I found is- in connectionDidFinishLoading obtain MIMEType from response and from that obtain file extension. I used below code, for the same:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
    CFStringRef mimeType = (__bridge CFStringRef)[_respo MIMEType];
    CFStringRef uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, mimeType, NULL);
    CFStringRef extension = UTTypeCopyPreferredTagWithClass(uti, kUTTagClassFilenameExtension);
    NSString *fileName = [NSString stringWithFormat:@"%@.%@", [[_respo suggestedFilename] stringByDeletingPathExtension], (__bridge NSString *)extension];
    [[NSFileManager defaultManager] createFileAtPath:[[self docsDir] stringByAppendingPathComponent:[NSString stringWithFormat:@"Downloads/%@", fileName]] contents:_downloadedData attributes:nil];
}

Now my questions are:

  1. Is there any other way to identify file extension for the files without extensions, say from NSData object?

  2. In place of checking file extension, is there any other way to show the file in appropriate view?

Please suggest.

解决方案

As a different approach - can you not simply look at the Content-type header of the connection you are using to download? If the server knows the mime type, that would seem to be a good solution. (If the server does not know the mime type but you have control of it, try installing: http://httpd.apache.org/docs/2.2/mod/mod_mime_magic.html or equivalent.)

这篇关于识别没有扩展名的文件的扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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