using System;
using System.Text;
using System.IO;
namespace Utility
{
/// <summary>
/// 文件下载帮助类
/// </summary>
public class DownloadFileHelper
{
/// <summary>
/// TransmitFile方式下载
/// 微软为Response对象提供了一个新的方法TransmitFile来解决使用Response.BinaryWrite
/// 下载超过400mb的文件时导致Aspnet_wp.exe进程回收而无法成功下载的问题。
/// </summary>
/// <param name="filepath">被下载文件的web服务器相对路径</param>
/// <returns></returns>
public static void DownloadByTransmitFile(string filepath)
{
try
{
string filename = GetFilenameByFilepath(filepath);
filename = Encoding.UTF8.GetString(Encoding.Default.GetBytes(filename));
if (filename.Length > 0)
{
string extension = filename.Split(',')[1].ToLower();
if (extension == "zip")
{
System.Web.HttpContext.Current.Response.ContentType = "application/x-zip-compressed";
}
else
{
System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
}
System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", filename));
filepath = System.Web.HttpContext.Current.Server.MapPath(filepath);
System.Web.HttpContext.Current.Response.TransmitFile(filepath);
}
}
catch (Exception)
{ }
}
/// <summary>
/// WriteFile方式下载
/// </summary>
/// <param name="filepath">被下载文件的web服务器相对路径</param>
public static void DownloadByWriteFile(string filepath)
{
try
{
string fileName = GetFilenameByFilepath(filepath);//客户端保存的文件名
fileName = Encoding.GetEncoding("gb2312").GetString(Encoding.Default.GetBytes(fileName));
string filePath = System.Web.HttpContext.Current.Server.MapPath(filepath);//路径
FileInfo fileInfo = new FileInfo(filePath);
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.ClearContent();
System.Web.HttpContext.Current.Response.ClearHeaders();
System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
System.Web.HttpContext.Current.Response.AddHeader("Content-Length", fileInfo.Length.ToString());
System.Web.HttpContext.Current.Response.AddHeader("Content-Transfer-Encoding", "binary");
System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
System.Web.HttpContext.Current.Response.WriteFile(fileInfo.FullName);
System.Web.HttpContext.Current.Response.Flush();
System.Web.HttpContext.Current.Response.End();
}
catch (Exception)
{ }
}
/// <summary>
/// WriteFile下载分块
/// </summary>
/// <param name="filepath">被下载文件的web服务器相对路径</param>
public static void ChunkDownloadByWriteFile(string filepath)
{
try
{
string fileName = GetFilenameByFilepath(filepath);//客户端保存的文件名
fileName = Encoding.UTF8.GetString(Encoding.Default.GetBytes(fileName));
string filePath = System.Web.HttpContext.Current.Server.MapPath(filepath);//路径
System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
if (fileInfo.Exists == true)
{
const long ChunkSize = 102400;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力
byte[] buffer = new byte[ChunkSize];
System.Web.HttpContext.Current.Response.Clear();
System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);
long dataLengthToRead = iStream.Length;//获取下载的文件总大小
System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
while (dataLengthToRead > 0 && System.Web.HttpContext.Current.Response.IsClientConnected)
{
int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小
System.Web.HttpContext.Current.Response.OutputStream.Write(buffer, 0, lengthRead);
System.Web.HttpContext.Current.Response.Flush();
dataLengthToRead = dataLengthToRead - lengthRead;
}
System.Web.HttpContext.Current.Response.Close();
}
}
catch (Exception)
{ }
}
/// <summary>
/// 流方式下载
/// </summary>
/// <param name="filepath">被下载文件的web服务器相对路径</param>
public static void DownloadByFileStream(string filepath)
{
try
{
string fileName = GetFilenameByFilepath(filepath);//客户端保存的文件名
fileName=Encoding.UTF8.GetString(Encoding.Default.GetBytes(fileName));
string filePath = System.Web.HttpContext.Current.Server.MapPath(filepath);//路径
//以字符流的形式下载文件
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
//通知浏览器下载文件而不是打开
System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
System.Web.HttpContext.Current.Response.BinaryWrite(bytes);
System.Web.HttpContext.Current.Response.Flush();
System.Web.HttpContext.Current.Response.End();
}
catch (Exception)
{ }
}
/// <summary>
/// 获取文件名称
/// </summary>
/// <param name="filepath">文件的web服务器的相对路径</param>
/// <returns></returns>
public static string GetFilenameByFilepath(string filepath)
{
string filename = "";
string[] array1 = filepath.Split('/');
try
{
string[] array2 = array1[array1.Length - 1].Split('?');
filename = array2[0];
}
catch (Exception)
{ }
return filename;
}
}
}