System.IO.FileInfo objFileInfo;
if (System.IO.File.Exists(filePath))
{
objFileInfo = new System.IO.FileInfo(filePath);
System.Web.HttpContext.Current.Response.Clear();
//告诉浏览器我们发送给它的是一个附件类型的文件
System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition ", "attachment;filename= " + objFileInfo.Name);
System.Web.HttpContext.Current.Response.AddHeader("Content-Length ", objFileInfo.Length.ToString());
//设置输出ContentType是“application/octet-stream”,即告诉浏览器要下载这个文件,而不是在浏览器中显示它
System.Web.HttpContext.Current.Response.ContentType = "application/msword";
System.Web.HttpContext.Current.Response.WriteFile(objFileInfo.FullName);
System.Web.HttpContext.Current.Response.End();
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "a ", " <Script> alert( '文件不存在! '); </Script> ");
}
下面是一个MIME类型的列表
“.asf”="video/x-ms-asf"
"avi"="video/avi"
".doc"="application/msword"
".zip"="application/zip"
".xls"="application/vnd.ms-excel"
".gif"="image/gif"
".jpg"="image/jpeg"
".wav"="audio/wav"
".mp3"="audio/mpeg3"
".mpg"/ "mpeg"="video/mpeg"
".rtf"="application/rtf"
".htm/html"="text/html"
".asp"="text/asp"
所有其他文件=“application/octet-stream”