c#(asp.net)实现的文件下载函数
本文导语: 完整代码如下。 代码示例: //文件下载函数 public static bool DownFile(string filePath) { try { if (File.Exists(filePath)) { HttpContext.Current.Response.Clear(); ...
完整代码如下。
//文件下载函数
public static bool DownFile(string filePath)
{
try
{
if (File.Exists(filePath))
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + filePath);
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.WriteFile(filePath);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Close();
HttpContext.Current.Response.End();
return true;
}
else
{
return false;
}
}
catch
{
return false;
}
}