我使用的是ASP.MVC,我希望允许用户从我的web服务器下载/查看文件。
这些文件不在此web服务器中。
我知道文件内容( byte[]数组),以及文件名。
我想要和网络侦探一样的行为。例如,如果mime类型是文本,我希望看到文本,如果是图像,同样,如果它是二进制,建议下载它。
做这件事最好的方法是什么?
谢谢你的进阶。
发布于 2010-12-01 13:37:46
图像的答案是可用的here
对于其他类型,必须从文件扩展名确定MIME类型。您可以使用Windows注册表或一些著名的哈希表,也可以使用IIS配置(如果在IIS上运行)。
如果计划使用注册表,下面是确定给定扩展名的MIME内容类型的代码:
public static string GetRegistryContentType(string fileName)
{
if (fileName == null)
throw new ArgumentNullException("fileName");
// determine extension
string extension = System.IO.Path.GetExtension(fileName);
string contentType = null;
using (Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(extension))
{
if (key != null)
{
object ct = key.GetValue("Content Type");
key.Close();
if (ct != null)
{
contentType = ct as string;
}
}
}
if (contentType == null)
{
contentType = "application/octet-stream"; // default content type
}
return contentType;
}https://stackoverflow.com/questions/4324759
复制相似问题