using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.IO; /// ///DownLoadFromServer 的摘要说明 /// public class DownLoadFromServer { /// /// 从服务器下载 /// public DownLoadFromServer() { // //TODO: 在此处添加构造函数逻辑 // } /// /// 下载Excele文件 /// /// 服务器存放Excel路径 public static void DownLoad(ref string ServerPath) { try { System.IO.FileInfo file = new System.IO.FileInfo(ServerPath); if (file.Exists == true) { System.Web.HttpContext.Current.Response.Clear(); System.Web.HttpContext.Current.Response.Charset = "GB2312"; System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8; // 添加头信息,为"文件下载/另存为"对话框指定默认文件名 System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + System.Web.HttpContext.Current.Server.UrlEncode("Sheet1.xlsx")); // 添加头信息,指定文件大小,让浏览器能够显示下载进度 System.Web.HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString()); // 指定返回的是一个不能被客户端读取的流,必须被下载 System.Web.HttpContext.Current.Response.ContentType = "application/ms-excel"; // 把文件流发送到客户端 System.Web.HttpContext.Current.Response.WriteFile(file.FullName); // 停止页面的执行 //Response.End(); System.Web.HttpContext.Current.Response.Flush(); //System.Web.HttpContext.Current.Response.End(); HttpContext.Current.ApplicationInstance.CompleteRequest(); } else { System.Web.HttpContext.Current.Response.Write(""); } } catch (Exception ex) { System.Web.HttpContext.Current.Response.Write(""); } } public static void DeleteExcel(ref string ServerPath) { System.IO.FileInfo file = new System.IO.FileInfo(ServerPath); if (file.Exists == true) { File.Delete(ServerPath); } } }