如何在运行时获取文件的内容/类型
我正在尝试动态确定输入文件的内容/类型。如果我在 Windows 应用程序中,我可以编写这样的代码(来自此博客)
private string GetContentType(string fileName) {
string contentType = "application/octetstream";
string ext = System.IO.Path.GetExtension(fileName).ToLower();
Microsoft.Win32.RegistryKey registryKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
if (registryKey != null && registryKey.GetValue("Content Type") != null)
contentType = registryKey.GetValue("Content Type").ToString();
return contentType;
}
还有哪些其他方法更适合 MVC 应用程序?
我想在 Controller.File(...)
方法中使用接收文件路径和内容类型的参数。
I am trying to determine dynamically the content/type of a input file. If I would be in a windows application I could write code like this (from this blog)
private string GetContentType(string fileName) {
string contentType = "application/octetstream";
string ext = System.IO.Path.GetExtension(fileName).ToLower();
Microsoft.Win32.RegistryKey registryKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
if (registryKey != null && registryKey.GetValue("Content Type") != null)
contentType = registryKey.GetValue("Content Type").ToString();
return contentType;
}
What other methods are more suitable for an MVC application?
I would like to use the param within the Controller.File(...)
method that receive a filepath and a contentype.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
在.Net 4.5中,您可以使用
MimeMapping.GetMimeMapping
:更多信息
In .Net 4.5 you can use
MimeMapping.GetMimeMapping
:More information
我只会使用文件扩展名,而不是尝试做一些聪明的事情,最终可能会反咬你一口。 :)
文件扩展名不需要在您的系统上注册(尽管我不知道您正在使用该文件做什么...)。如果您想过滤掉文件,您可以使用诸如枚举或数据库表之类的内容,其中包含有关可接受的扩展名的信息。
请参阅@Tolgahan 的想法。我基于此创建了一个下面的 C# 枚举,如果人们希望为此创建一个基于 db/enum/xml 的方法,它应该为人们提供一个起点。
I would just use the file extension rather than try to do something clever that may eventually come back to bite you in the arse. :)
The file extension doesn't need to be registered on your system (although I don't know exactly what you're doing with the file...). You could use something like an enum or db table which contains information on acceptable extensions if you want to filter out files.
Please see @Tolgahan's idea on this. I created a C# enum below based on this which should provide people with a starting point should they wish to create a db/enum/xml-based approach to this.
.NET CORE
使用 Microsoft.AspNetCore.StaticFiles
.NET CORE
Using Microsoft.AspNetCore.StaticFiles
IDEA:放置格式和扩展数据(http://www.feedforall.com/mime-types.htm) htm) 到 xml 或您的项目中作为字典、数组或其他内容进行查询,并创建一个过程来确定扩展的 mimetype。我认为将数据保存在 xml 文档中将更容易在编译后进行修改
IDEA: put the formats and extensions data (http://www.feedforall.com/mime-types.htm) into an xml or into your project as dictionary, array or sth else for query and create a procedure for determine mimetype for extension.. i think keeping your data on xml document will be easier for modification after compiling
如果你想使用流
If you want to use stream
文件是否正在上传到 MVC 应用程序?
不过,有一些警告。
它使用浏览器提供的又名“客户端提供的”mime 类型,如果上传项目的用户未经身份验证或信任,那么这可能不是一个好主意。
此外,IE 并不总是为某些文件提供标准 Mime 类型,特别是 PNG(请参阅 “image/png”和“image/x-png”之间有什么区别?)这可能会也可能不会影响您的应用程序。
我们只是使用一个简单的 if 语句将 IE png mime 类型 (image/x-png) 转换回更常见的类型 (image/png)。
Is the file being uploaded to the MVC application?
There are a couple of caveats with this though.
It uses the browser supplied aka 'client supplied' mime type, if the users uploading items aren't authenticated or trusted, then this might not be a good idea.
Also IE doesn't always give standard Mime types for certain files, in particular PNGs (see What is the difference between "image/png" and "image/x-png"?) which may or may not effect your application.
We just use a simple if statement to convert the IE png mime type (image/x-png), back to the more common one (image/png).
如果您使用的是类似 linux/unix 的系统,您可以使用
file
命令来确定文件类型。如果您使用的是 Windows 计算机,通常会使用代码中所示的文件扩展名。
If you are on a linux/unix like system you can use the
file
command to determine the file type.If you're on a Windows machine it is common to use the file extension like you showed in your code.