0
我有一个网站,允许用户上传文档,如PDF,WORD,EXCEL等在我的代码正确使用Response.ContentType
现在,当我想以显示文档在浏览器中,我我正在检查扩展并适当地设置ContentType。
但是,这个switch语句听起来并不像最好的解决方案。
有没有一种方法我可以设置基于该Stream
内容类型?
我有一个网站,允许用户上传文档,如PDF,WORD,EXCEL等在我的代码正确使用Response.ContentType
现在,当我想以显示文档在浏览器中,我我正在检查扩展并适当地设置ContentType。
但是,这个switch语句听起来并不像最好的解决方案。
有没有一种方法我可以设置基于该Stream
内容类型?
如果你不想使用像语句开关,使用字典添加可能MIME
-entries。
Dictionary<string, string> map=new Dictionary<string, string>
{
{"txt", "text/plain"},
{"exe", "application/octet-stream"},
{"bmp", "image/bmp"}
};
,并设置内容类型,
Response.ContentType=map[ext];
我发现一个有趣的计算器线程 - Using .NET, how can you find the mime type of a file based on the file signature not the extension
AVD,感谢。我猜你的答案,然后有没有真正的方法来推断从字节数组或流的类型? – griegs
@griegs - 请检查我添加的链接。 – adatapost
谢谢你。我无法使用Urlmon,但基于您的字典解决方案,我找到了这个方便的课程。 https://github.com/cymen/ApacheMimeTypesToDotNet/blob/master/ApacheMimeTypes.cs – griegs