using Microsoft.Win32;
string filename=File1.Value;//这里使用了一个HtmlInputFile
System.IO.FileInfo fi=new System.IO.FileInfo(filename);
string fileextname=fi.Extension;
string DEFAULT_CONTENT_TYPE = "application/unknown";
RegistryKey regkey,fileextkey;
string filecontenttype;
try
{
regkey=Registry.ClassesRoot;
fileextkey=regkey.OpenSubKey(fileextname);
filecontenttype=fileextkey.GetValue("Content Type",DEFAULT_CONTENT_TYPE).ToString();
}
catch
{
filecontenttype=DEFAULT_CONTENT_TYPE;
}
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(filename));
Response.ContentType=filecontenttype;
Response.WriteFile(filename);
System.IO.FileInfo fi=new System.IO.FileInfo(filename);
string fileextname=fi.Extension;
string DEFAULT_CONTENT_TYPE = "application/unknown";
RegistryKey regkey,fileextkey;
string filecontenttype;
try
{
regkey=Registry.ClassesRoot;
fileextkey=regkey.OpenSubKey(fileextname);
filecontenttype=fileextkey.GetValue("Content Type",DEFAULT_CONTENT_TYPE).ToString();
}
catch
{
filecontenttype=DEFAULT_CONTENT_TYPE;
}
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(filename));
Response.ContentType=filecontenttype;
Response.WriteFile(filename);
西部e网(weste.net)编辑:经过测试,这段程序还真是不错~~~节省了switch-case好大一段代码,也解决了比如图片、文本等不提示下载而直接显示的问题。