using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
/// <summary>
/// 文件下载
/// </summary>
public class DownLoad
{
/// <summary>
/// 文件下载类型
/// </summary>
public enum FileType {
xls,
doc,
txt,
zip
}
/// <summary>
/// 文件下载
/// </summary>
/// <param name="FullFileName"></param>
/// <param name="DisplayName"></param>
/// <param name="fileType"></param>
/// <param name="callPage"></param>
public static void Run(string FullFileName,string DisplayName,FileType fileType, System.Web.UI.Page callPage)
{
try
{
FileStream fsZ = File.OpenRead(@FullFileName);
byte[] bufferZ = new byte[fsZ.Length];
fsZ.Read(bufferZ, 0, bufferZ.Length);
fsZ.Close();
callPage.Response.Clear();
callPage.Response.Buffer = true;
callPage.Response.Charset = "utf-8";
switch (fileType)
{
case FileType.doc:
callPage.Response.ContentType = "application/ms-word";
DisplayName += ".doc";
break;
case FileType.xls:
callPage.Response.ContentType = "application/vnd.ms-excel";
DisplayName += ".xls";
break;
case FileType.txt:
callPage.Response.ContentType = "application/ms-txt";
DisplayName += ".txt";
break;
case FileType.zip:
callPage.Response.ContentType = "application/zip";
DisplayName += ".zip";
break;
}
callPage.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(@DisplayName.Trim(), System.Text.Encoding.UTF8));
callPage.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");//设置输出流为简体中文
callPage.Response.BinaryWrite(bufferZ);
//callPage.Response.End();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}