#region 下载文件
/// <summary>
/// 下载文件
/// </summary>
/// <param name="downName">客户端下载时候的名字 不需要文件后缀</param>
/// <param name="serverPath">文件路径例如 根目录下 "../test/test.txt"</param>
public static void DonwloadFile(string downName, string serverPath)
{
string filePath = serverPath;
if (!serverPath.Contains(":"))
{
filePath = HttpContext.Current.Server.MapPath(serverPath);//路径
}
string fileExtensioin = Path.GetExtension(filePath);
string fileName = downName + fileExtensioin;//客户端保存的文件名
//以字符流的形式下载文件
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
File.Delete(filePath);//此处用于导出文件夹后的删除,根据自己的需要
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = "application/octet-stream";
//通知浏览器下载文件而不是打开
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
HttpContext.Current.Response.BinaryWrite(bytes);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
#endregion
/// <summary>
/// 下载文件
/// </summary>
/// <param name="downName">客户端下载时候的名字 不需要文件后缀</param>
/// <param name="serverPath">文件路径例如 根目录下 "../test/test.txt"</param>
public static void DonwloadFile(string downName, string serverPath)
{
string filePath = serverPath;
if (!serverPath.Contains(":"))
{
filePath = HttpContext.Current.Server.MapPath(serverPath);//路径
}
string fileExtensioin = Path.GetExtension(filePath);
string fileName = downName + fileExtensioin;//客户端保存的文件名
//以字符流的形式下载文件
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
File.Delete(filePath);//此处用于导出文件夹后的删除,根据自己的需要
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = "application/octet-stream";
//通知浏览器下载文件而不是打开
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
HttpContext.Current.Response.BinaryWrite(bytes);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
#endregion