Files
2-BeiqiFutian-MES_Manage/App_code/DownLoadFromServer.cs
2026-05-29 13:44:19 +08:00

69 lines
2.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
/// <summary>
///DownLoadFromServer 的摘要说明
/// </summary>
public class DownLoadFromServer
{
/// <summary>
/// 从服务器下载
/// </summary>
public DownLoadFromServer()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 下载Excele文件
/// </summary>
/// <param name="ServerPath">服务器存放Excel路径</param>
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("<script>alert('请尽快与管理员联系.')</script>");
}
}
catch (Exception ex)
{
System.Web.HttpContext.Current.Response.Write("<script>alert('系统出现以下错误://n" + ex.Message + "!//n请尽快与管理员联系.')</script>");
}
}
public static void DeleteExcel(ref string ServerPath)
{
System.IO.FileInfo file = new System.IO.FileInfo(ServerPath);
if (file.Exists == true)
{
File.Delete(ServerPath);
}
}
}