1
我正在使用ASP MVC,我想允许用户从我的Web服务器下载/查看文件。Web:从FileName和FileContent中查看文件的原始内容
这些文件不在本网站服务器上。
我知道文件内容(一byte[]
阵列),也是文件名。
我想要与Web Broswer相同的行为。例如,如果MIME类型是文本,我希望看到文本,如果它是一个图像,如果它是二进制文件,则建议它下载。
这样做的最好方法是什么?
谢谢先进。
我正在使用ASP MVC,我想允许用户从我的Web服务器下载/查看文件。Web:从FileName和FileContent中查看文件的原始内容
这些文件不在本网站服务器上。
我知道文件内容(一byte[]
阵列),也是文件名。
我想要与Web Broswer相同的行为。例如,如果MIME类型是文本,我希望看到文本,如果它是一个图像,如果它是二进制文件,则建议它下载。
这样做的最好方法是什么?
谢谢先进。
对图像的回答,请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;
}
我的Web服务器将IIS或XSP(单声道),所以我不能假设我可以访问Windows注册表:-( – 2010-12-01 13:42:27