文件下载
来源:互联网 发布:js添加dom元素 编辑:程序博客网 时间:2024/05/15 23:49
public static void DownloadFile(string filepath)
{
string strContentType = "";
string strFileName = "";
string strFilePath = "";
int intIndex1 = -1;
int intIndex2 = -1;
int intIndex = 0;
if (filepath.EndsWith(".doc"))
{
strContentType = "application/msword";
}
else if (filepath.EndsWith(".xls"))
{
//strContentType = "application/x-zaurus-xls"
strContentType = "application/vnd.ms-excel";
}
else if (filepath.EndsWith(".pdf"))
{
strContentType = "application/pdf";
}
else if (filepath.EndsWith(".ppt"))
{
//strContentType = "application/powerpoint"
strContentType = "application/vnd.ms-powerpoint";
}
else if (filepath.EndsWith(".jpg") || filepath.EndsWith(".jpeg") || filepath.EndsWith(".jpe"))
{
strContentType = "image/jpeg";
}
else if (filepath.EndsWith(".bmp"))
{
strContentType = "image/bmp";
}
else if (filepath.EndsWith(".gif"))
{
strContentType = "image/gif";
}
else if (filepath.EndsWith(".zip"))
{
strContentType = "application/zip";
}
else if (filepath.EndsWith(".txt"))
{
strContentType = "text/plain";
}
else
{
strContentType = "application/octet-stream";
}
intIndex1 = filepath.LastIndexOf("/");
intIndex2 = filepath.LastIndexOf("//");
if (intIndex1 > intIndex2)
{
intIndex = intIndex1;
}
else
{
intIndex = intIndex2;
}
if (intIndex > -1)
{
if (intIndex + 1 < filepath.Length)
strFileName = filepath.Substring(intIndex + 1);
}
else
{
strFileName = filepath;
}
if (filepath.StartsWith("/") || filepath.StartsWith("//"))
strFilePath = HttpContext.Current.Server.MapPath("/") + filepath;
else
strFilePath = filepath;
if (FileUtil.isFileExist(strFilePath))
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = strContentType;
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(strFileName));
HttpContext.Current.Response.WriteFile(strFilePath);
//HttpContext.Current.Response.BinaryWrite(objFileIO.IFileByte(strFilePath));
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Close();
}
else
{
SetGlobalMessageID(FindKeyValueCutNull(GXML.COMMONMESSAGE, GMSG.MSG904));
}
}
- 下载图片,下载文件
- 文件下载
- 文件下载
- 文件下载
- 文件下载
- 文件下载
- 文件下载
- 下载文件
- 文件下载
- 下载文件
- 文件下载
- 文件下载
- 下载文件
- 文件下载
- 下载文件
- 下载文件
- 文件下载
- 下载文件
- 报表程序无法执行也无法删除的一种解决办法
- 北京春运期间停售部分返程票 预售期将有所调整
- test
- 用结构体解某年某月某日是当年的第几天
- javascript 有用的 代码(转转)
- 文件下载
- 单向动态链表的创建、输入、插入、删除、输出
- 求两个整数的最大公约数和最小公倍数
- 啥都干,啥都不会。
- 解决Struts2乱码问题基本方法
- 用GetModuleFileName获取程序当前执行路径
- 跳槽手册:CIO如何“挑选”老板
- 获取天气预报c#
- ASP.net 2.0 中 WebResource.axd 管理资源的一些知识点